Jan 11, 2022 at 11:50pm UTC
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 Jan 11, 2022 at 11:51pm UTC
Jan 12, 2022 at 4:17pm UTC
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 Jan 12, 2022 at 4:19pm UTC
Jan 12, 2022 at 4:52pm UTC
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 Jan 12, 2022 at 4:53pm UTC
Jan 12, 2022 at 5:45pm UTC
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.
Jan 12, 2022 at 5:47pm UTC
Hi Ganado
"myproject" and "mylibrary" have the same parent directory called "cpp_projects".
Jan 12, 2022 at 5:59pm UTC
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 Jan 12, 2022 at 6:01pm UTC
Jan 12, 2022 at 8:33pm UTC
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.
Jan 13, 2022 at 7:45am UTC
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 Jan 13, 2022 at 3:30pm UTC