compiling / linking problem

Hi guys

i have a directory "mylibrary" with this sub-directories:
> include
>> file.hpp
>> console.hpp
> source
>> file.cpp
>> console.cpp

and i have another directory "myproject" with
> include
>> math1.hpp
>> math2.hpp
> source
>> math1.cpp
>> math2.cpp
myproject.cpp


How can i compile/linking myproject.cpp, in g++, using sources from mylibrary and myproject ?
Last edited on
Just make sure all the correct paths to source and include files are specified in your makefile.

EDIT: Or in your CMake files, or in whatever other build system you're using.
Last edited on
Are the "myproject" and "mylibrary" folders both within the same parent directory? If not, what is the difference in path from their common ancestor?
Last edited on
Hi MikeyBoy

I don't know how to work with CMake and i don't have a build system. I am just using g++ command line to compile stuff.
Hi Ganado

"myproject" and "mylibrary" have the same parent directory called "cpp_projects".
I do suggest using things like makefiles, because at the very least it can keep track of when files change, and only rebuild what's necessary.

The following assumes you are running the command from the myproject directory, and that 'myproject.cpp' is a child of myproject directory (you don't have >> next to in your OP).

If you want a simple one-liner, it's:

g++ -Wall -Wextra -Wpedantic myproject.cpp source/math1.cpp source/math2.cpp ../mylibrary/source/console.cpp ../mylibrary/source/file.cpp -o prog



You can also break this up into multiple commands so that it builds separate object files for each compilation unit (each .cpp file). This is used with a makefile system to prevent re-compilation of units that have not changed.
https://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files
g++ -c myclass.cpp
g++ -c main.cpp
g++ -o prog myclass.o main.o
Last edited on
yea if its big enough to have more than a file or two, its big enough for a project.
I keep a trash batch file to build little examples around, its nice, but very few 'real' programs will be built that way -- maybe some simple personal utility program.
I'm assuming linux (or posix) using gcc, and not Windows.

Also, assuming mylibrary and myprojects have the same parent directory.

In "mylibrary", you need need build.sh:
1
2
3
4
5
#!/usr/bin/env bash

g++ -fpic -std=c++17 -Iinclude -c source/file.cpp -o source/file.o && \
g++ -fpic -std=c++17 -Iinclude -c source/console.cpp -o source/console.o && \
g++ -shared -o libmylibrary.so source/file.o source/console.o


In "myproject", you need build.sh:
1
2
3
4
5
6
#!/usr/bin/env bash

g++ -std=c++17 -I../mylibrary/include -c source/math1.cpp -o source/math1.o && \
g++ -std=c++17 -I../mylibrary/include -c source/math2.cpp -o source/math2.o && \
g++ -std=c++17 -I../mylibrary/include -c myproject.cpp -o source/myproject.o && \
g++ -o myproject source/math1.o source/math2.o source/myproject.o -L../mylibrary -lmylibrary
Hi Ganado and kbw

Thanks both for tips. I used combination of your tips. It compile my main.cpp file in Windows. I runned this command line in "cpp_projects" directory.



g++ -std=c++17 -Wall -Wextra -Wpedantic 
myproject/main.cpp 
mylibrary/source/*.cpp 
-I myproject/source/*.cpp 
-I mylibrary/include 
-I myproject/include 
-o myproject/main.exe





Last edited on
Registered users can post here. Sign in or register to post.