User forums > Using Code::Blocks
Using classes across projects
kerui:
Hello,
I am building a large application and I want to separate the components of this application to make my task easier. I have created two projects within the Code::Blocks workspace. I want the classes in the second project to be able to use the classes in the first project. Both projects are "console applications" and I selected the first project as a dependency of the second.
I can compile and generate the object files (and executable) of the first project without problem (it has no user-defined dependences). But when the second project attempts to build, the compiler complains of undefined references. I have added the "obj/Debug/" directory of the first project in the "Properties->Build Options->Search Directories" of the second project, but to no avail.
Why can't I get the second project to locate the object files when they're present in the directory I have specified?
Do I have to create the first project as a library to get this to work?
Is there a better way to organise large applications in Code::Blocks rather than having lots of classes in one directory?
Any help much appreciated!
oBFusCATed:
Because this is the wrong way of doing things.
What you should really do is extract the common files in a library (static or shared) and then link the library to both projects.
zabzonk:
Although building a library is the best way to go, you can add the .o files to your other project. Assume you want to add the files from project A to project B, then open project B, go Project|Build Options|Linker Settings and add the .o files in project A to the Link Libraries box. You will also have to modify the Search Directories|Compiler list in the same dialog so that the header files will get picked up when you include them.
kerui:
Thanks for the replies.
--- Quote ---Assume you want to add the files from project A to project B, then open project B, go Project|Build Options|Linker Settings and add the .o files in project A to the Link Libraries box.
--- End quote ---
This sounds good. Creating the first project as a library doesn't seem right in my circumstances.
Say you were writing a game and you wanted your game entity classes in one directory, with the game "mechanics" in another. It wouldn't work to make the game entities a library in that case. But what if you wanted to organise dozens of classes your game into components and test separately?
This is more akin to what I'm doing. Is it the wrong way to create separate projects for these components? Is there a better way in Code::Blocks? (I couldn't locate information about this in the documentation)
mirai:
You may want to choose source level library approach, especially if you develop shared modules along with project-specific modules simultaneously. The idea is to put source file of shared modules to a common directory and include them in all dependent projects directly without explicit creation of a library. This source level library approach is straightforward and simple. However, static/dynamic library approach with correct project dependencies is more or less of a standard way of doing these things.
Navigation
[0] Message Index
[#] Next page
Go to full version