Page 1 of 1

Adding my own C++ headers from other project

Posted: Thu Feb 20, 2020 5:47 pm
by DonQ104
I'm making an application in 2 version. One in the console mode and second with GUI using wxWidgets. I have already finished the first one and i want to make that with GUI using code from the first. I want to connect functions to buttons. How can I connect the header files from the first app to a wxWidgets project to compile properly? I have added them to the project like in standar Code Blocks project but compiler, when I try to create object, says that i have undefined reference to construcor and destructor. What I need to change to compile my program?

Re: Adding my own C++ headers from other project

Posted: Thu Feb 20, 2020 6:06 pm
What is your wx version?
What platform?
How did you compile wxWidgets?
Are you using C::B or just trying to compile the program from the Terminal?
Which code are you trying to compile?
What command you are using to compile the application?
What is the exact error message(s)?

Unfortunately nobody here have a Crystal Ball to look at your environment and your program and tell what is wrong. You need to provide the information above in order to get a chance to get help.

Thank you.

Re: Adding my own C++ headers from other project

Posted: Thu Feb 20, 2020 8:58 pm
by New Pagodi
I'm not sure what your asking, but here's an very simple example of how you can use the same code file in 2 projects with codeblocks. I created a console application and a wxWidgets application in the same folder. Then I used the new file wizard to create a c++ header and a c++ file named class.h and class.cpp respectively. Then I added those files to the wxWidgets project:
Untitled.png (16.83 KiB) Viewed 352 times
The contents of class.h are

Code: Select all


#include <string>

class MyClass
        std::string GetString() const;

        std::string m_string;


The contents of class.cpp

Code: Select all

#include "class.h"

    m_string = "hello";

std::string MyClass::GetString() const
    return m_string;
In the console application, I changed main.cpp to

Code: Select all

#include <iostream>
#include "class.h"

int main()
    MyClass c;

    std::cout << c.GetString() << std::endl;
    return 0;
And in the wxWidgets application, I changed the <projectname>Main.cpp file by adding

Code: Select all

#include "class.h"
near the start and in the constructor for the form, I added

Code: Select all

    MyClass c;
This is a really simple, pointless example, but it shows how to share a code file between 2 projects.

One thing to watch out for is that both projects will compile the file class.cpp but with different compile options. The keep the object files separate, I changed the object folder for the console application.
Untitled1.png (39.62 KiB) Viewed 352 times
The same change should be made for the release target.

Another, possibly better option would be to separate the the common parts of the console and gui applications into static library project. Then add the resulting library to the link objects for both the console and gui applications.

Re: Adding my own C++ headers from other project

Posted: Thu Feb 20, 2020 9:23 pm
by DonQ104
@New Pagodi thank you solving my problem. WIth these screenshots everything makes clear right now :D