Author Topic: Using classes across projects  (Read 3540 times)

Offline kerui

  • Single posting newcomer
  • *
  • Posts: 5
Using classes across projects
« on: November 28, 2011, 06:12:19 pm »
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!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12005
    • Travis build status
Re: Using classes across projects
« Reply #1 on: November 28, 2011, 06:26:21 pm »
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.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

zabzonk

  • Guest
Re: Using classes across projects
« Reply #2 on: November 28, 2011, 06:36:13 pm »
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.

Offline kerui

  • Single posting newcomer
  • *
  • Posts: 5
Re: Using classes across projects
« Reply #3 on: November 28, 2011, 07:42:16 pm »
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.

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)

Offline mirai

  • Multiple posting newcomer
  • *
  • Posts: 101
Re: Using classes across projects
« Reply #4 on: November 28, 2011, 11:35:57 pm »
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.

Offline kerui

  • Single posting newcomer
  • *
  • Posts: 5
Re: Using classes across projects
« Reply #5 on: November 29, 2011, 12:24:29 am »
Quote
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.

How can I do that in Code::Blocks?

As I mentioned, I tried giving the project the required "search directories" for the headers and object files, but I still get a linking error. I got rid of the linking error by specifying each object file individually using the "Linker" settings but I don't understand why this should be necessary just because the object files are in a separate directory.

Why can't the compiler link objects in different directories when I have specified the directories to search?


Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12005
    • Travis build status
Re: Using classes across projects
« Reply #6 on: November 29, 2011, 12:48:38 am »
If you don't make a library you'll have to:
1. add all cpp files in both projects, which will compile them n-times
2. add the .o files to the link additional linker options (I've never tried this because it is a hack), this approach is not recommended, because to get the proper setup you'll have to setup the correct external dependencies, so the project is correctly relinked if the .o files change.

If you don't want to bother with static lib go for solution 1. It is the easiest and safer approach. Then is your build starts to take too long, invest some time and separate the code in a static library.
If you go the number 2 route sooner or later you'll switch to number 1 or the static library.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline kerui

  • Single posting newcomer
  • *
  • Posts: 5
Re: Using classes across projects
« Reply #7 on: November 29, 2011, 01:41:27 am »
Quote
1. add all cpp files in both projects, which will compile them n-times

Okay. Say I was building a game, and I had classes for "AI", "Graphics", "Game Entities", "Players" etc. If I put those class files all together in one directory, could Code::Blocks help me organise the "Class View Hierarchy" in the IDE so that classes could be placed in these components?

Is this when the "virtual folder" feature is used?

(It wouldn't be viable to create "Game Entities" or "Players" as libaries because a class in the "AI" component of the game requires an array of "game entities".)

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9496
Re: Using classes across projects
« Reply #8 on: November 29, 2011, 08:43:31 am »
Is this when the "virtual folder" feature is used?
Yes.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12005
    • Travis build status
Re: Using classes across projects
« Reply #9 on: November 29, 2011, 08:51:25 am »
Is this when the "virtual folder" feature is used?
Why don't you use real folders in the file system?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline kerui

  • Single posting newcomer
  • *
  • Posts: 5
Re: Using classes across projects
« Reply #10 on: November 29, 2011, 10:14:57 am »
Quote
Why don't you use real folders in the file system?

It's okay now, thanks. I used the "add file" feature, and Code::Blocks automatically organises the hierarchy into the project folders. Everything compiles and links properly now and the view is organised.