So i understand that i need to link my header files which are located in the include folder i also understand that i need to link the lib files in the lib folder.
But i am stuck.
For example if i want to include sfml and use it in my project do i just need to do the following add_library(root/lib) and include_directories(root/include/SFML).
This is my hieracy:
Videos
Recently I'm cleaning up the cmakelist.txt file of my project. I found out when using CLion, it automatically adds both my header and source files to add_executable command in cmake. But when I read some open sourced projects on GitHub. I found out they include the headers by target_link_directories, or create a library from the directory including both headers and sources. May I ask about the rationale behind those choices? thanks
This solution seemed to work.
add_library(library1 SHARED
file1.cpp
file2.cpp
)
add_library(library2 SHARED
file3.cpp
file4.cpp
)
# dummy file is required to avoid a cmake error, but this
# "dummy" file serves no other purpose and is empty.
add_library(master_library SHARED
dummy.cpp
)
# Link the master library with the other libraries
target_link_libraries(master_library
library1
library2
)
After doing this, I was able to compile and link code using ONLY the master library.
If all you want is a convenient target for use by others and don't care about whether you have one or multiple libraries, cmake can do that with an interface library:
add_library(library1 SHARED
file1.cpp
file2.cpp
)
add_library(library2 SHARED
file3.cpp
file4.cpp
)
add_library(master_library INTERFACE)
# Link the master library with the other libraries
target_link_libraries(master_library INTERFACE
library1
library2
)
Then in another location if you have
target_link_libraries(my_executable PRIVATE
master_library
)
my_executable will link against library1 and library2
I am following the official cmake tutorial and am currently at Step 2:
Step 2: Adding a Library — CMake 3.27.4 Documentation
Here, a library is created in a child directory. Then, in the parent directory, it is added as a subdirectory. Then, the library is linked to.
Why is there a need to do this in 3 separate steps? I come from a Visual Studio IDE background where if my co-programmer has written a specialized code he is incharge of. All I need to do to incorporate it in my code is to add his header and implementation file to VSIDE and build the entire project. This will create .obj files for my own .cpp files as well as his new .cpp implementation files and linking happens at the linker stage without the need to specify any special library.
The only linking that I have had to do are those cases where I do not have the .cpp implementation file. The author of the library provides header files that define various objects. Then, I have to specify the .lib file in the linker section of the IDE and the project successfully builds.
So, what is the need of 3 separate stages in CMake, when one already has the .cpp files from the library author?