User forums > General (but related to Code::Blocks)
Depedency between projects issue
GoussLegend:
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;
}
--- End code ---
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_ */
--- End code ---
cacb:
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.
GoussLegend:
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?
cacb:
--- Quote from: GoussLegend 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?
--- End quote ---
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.
stahta01:
--- Quote from: cacb on September 28, 2014, 05:28:28 pm ---
--- Quote from: GoussLegend 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?
--- End quote ---
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.
--- End quote ---
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.
Navigation
[0] Message Index
[#] Next page
Go to full version