Author Topic: Depedency between projects issue  (Read 8780 times)

Offline GoussLegend

  • Single posting newcomer
  • *
  • Posts: 2
Depedency between projects issue
« on: September 28, 2014, 07:33:00 am »
Hi,

I am new to C++ and Code::Blocks. I have a project myProject and I have another project myProjectTesting, which depends on myProject.

I have setup myProject as a dependency for myProjectTesting, as explained in the Code::Blocks documentation: http://wiki.codeblocks.org/index.php?title=The_build_process_of_Code::Blocks#Using_project_dependencies

myProjectTesting contains only one file with the following code:

Code
#include "Position.hpp"

int main()
{
    Position position(3, 4);

    return 0;
}

Position.hpp is a class header in myProject. I have the following error when compiling with gcc:

undefined reference to `Position::Position(int, int)'.

Note that the line Position position(3, 4); runs fine If I use it inside myProject.


The Position.hpp file is the following:

Code
/*
 * Position.hpp
 *
 *  Created on: 30/08/2014
 *      Author: Romain
 */

#ifndef POSITION_H_
#define POSITION_H_
#include <string>

class Position
{
public:

    Position(int _x, int _y);
    Position();
    Position deltaX(int delta);
    Position deltaY(int delta);
    Position deltaXY(int deltaX, int deltaY);
    int getX();
    int getY();
    std::string print();

private:

    int x;
    int y;
};

#endif /* POSITION_H_ */

Offline cacb

  • Lives here!
  • ****
  • Posts: 539
Re: Depedency between projects issue
« Reply #1 on: September 28, 2014, 10:27:46 am »
When you declare (in Code::Blocks) a project as being dependent on another project, it only affects the order of the project builds, i.e. myProject is built before myProjectTesting in your case.

You still have to manually add the library myProject to the libraries being linked to from myProjectTesting, or else you will experience unresolved symbols, like you have seen.

Offline GoussLegend

  • Single posting newcomer
  • *
  • Posts: 2
Re: Depedency between projects issue
« Reply #2 on: September 28, 2014, 03:04:16 pm »
Thanks for your answer. I have tried it and it works well!  :)

I have still another question though: What is the best practice to do that? Create a build target which will generate the librairy and have another build target to generate the .exe file?

Offline cacb

  • Lives here!
  • ****
  • Posts: 539
Re: Depedency between projects issue
« Reply #3 on: September 28, 2014, 05:28:28 pm »
Thanks for your answer. I have tried it and it works well!  :)

I have still another question though: What is the best practice to do that? Create a build target which will generate the librairy and have another build target to generate the .exe file?


That does not seem right to me.
The source codes of the library and the application are different, and belong in separate projects. The library should be a e.g. a static library project and the application an executable project, each with their own source files. Then declare dependency on the library project and link against the library (in each target).

It is more natural IMHO to use build targets for Debug/Release settings.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7678
    • My Best Post
Re: Depedency between projects issue
« Reply #4 on: September 28, 2014, 05:34:54 pm »
Thanks for your answer. I have tried it and it works well!  :)

I have still another question though: What is the best practice to do that? Create a build target which will generate the librairy and have another build target to generate the .exe file?


That does not seem right to me.
The source codes of the library and the application are different, and belong in separate projects. The library should be a e.g. a static library project and the application an executable project, each with their own source files. Then declare dependency on the library project and link against the library (in each target).

It is more natural IMHO to use build targets for Debug/Release settings.

For some projects, it is easier to have the Library be just a separate Target.
Note: If you have NOT yet decided which is best you can make a  separate Target in CB into a separate project very easily.

The CB Core project has several libraries in it as separate Targets; the likely reason is that it is easier to distribute it that way.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline cacb

  • Lives here!
  • ****
  • Posts: 539
Re: Depedency between projects issue
« Reply #5 on: September 28, 2014, 08:19:55 pm »
For some projects, it is easier to have the Library be just a separate Target.

Really? How do you declare dependencies between targets? Can a project reuse a library defined as a Target  in another project? How about dependencies in those cases?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7678
    • My Best Post
Re: Depedency between projects issue
« Reply #6 on: September 28, 2014, 08:34:44 pm »
For some projects, it is easier to have the Library be just a separate Target.

Really? How do you declare dependencies between targets? Can a project reuse a library defined as a Target  in another project? How about dependencies in those cases?

Have you ever compiled Code::Blocks (CB) using CB projects to do it?

If not, look at the main CB Project it has at least 3 libraries in it; and, it has multiple executable targets that use them.

Edit1: And, most of the CB Contrib projects use the CB DLL created by the CB Core project!

Edit2: I suggest looking up CB "Virtual targets" and external file dependency as things you need to know about CB to do what I suggested.

Tim S.
 
« Last Edit: September 28, 2014, 08:41:53 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline cacb

  • Lives here!
  • ****
  • Posts: 539
Re: Depedency between projects issue
« Reply #7 on: September 29, 2014, 12:04:02 am »
Quote
Really? How do you declare dependencies between targets? Can a project reuse a library defined as a Target  in another project? How about dependencies in those cases?

Have you ever compiled Code::Blocks (CB) using CB projects to do it?

No, on Linux I build C::B from Jens Lody tarballs and on Windows I use MSVC compiler...

If not, look at the main CB Project it has at least 3 libraries in it; and, it has multiple executable targets that use them.

Edit1: And, most of the CB Contrib projects use the CB DLL created by the CB Core project!
Edit2: I suggest looking up CB "Virtual targets" and external file dependency as things you need to know about CB to do what I suggested.

I may do all this one day, but simple answers to simple questions would indeed be simpler. I know about external file dependency (I have used it), but it isn't very easy IMHO.