Adding my own C++ headers from other project Topic is solved

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
DonQ104
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Feb 20, 2020 5:37 pm

Adding my own C++ headers from other project

Post by DonQ104 » Thu Feb 20, 2020 5:47 pm

Hello
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?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3893
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

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

Post by ONEEYEMAN » Thu Feb 20, 2020 6:06 pm

Hi,
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.

New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 327
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

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

Post by New Pagodi » Thu Feb 20, 2020 8:58 pm

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
Untitled.png (16.83 KiB) Viewed 134 times
The contents of class.h are

Code: Select all

#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED

#include <string>

class MyClass
{
    public:
        MyClass();
        std::string GetString() const;

    private:
        std::string m_string;

};

#endif // CLASS_H_INCLUDED
The contents of class.cpp

Code: Select all

#include "class.h"

MyClass::MyClass()
{
    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;
    SetTitle(c.GetString());
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
Untitled1.png (39.62 KiB) Viewed 134 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.

DonQ104
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Feb 20, 2020 5:37 pm

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

Post by DonQ104 » Thu Feb 20, 2020 9:23 pm

@New Pagodi thank you solving my problem. WIth these screenshots everything makes clear right now :D

Post Reply