Reputation: 155
I have reorganised my project and I'd like to know how to include the source code from subdirectories in the build of my shared object. I now have the following directory structure:
src/
Component/
CMakeLists.txt
SubComponent1/
src1.h
src1.cpp
SubComponent2/
src2.h
src2.cpp
My CMakeLists.txt currently looks like:
add_library(MainProject SHARED src1.cpp src2.cpp)
How do I now update it so that it will build the folders below? Do I also need to add CMakeLists.txt to the SubComponentX folders?
Thank you
Upvotes: 2
Views: 403
Reputation: 15966
If you still want the sources to be buit into a single library, just change the paths in your project:
add_library(MainProject SHARED
SubComponent1/src1.cpp
SubComponent2/src2.cpp
)
If you want each of your subdirectories to build separately, you can use add_subdirectory
in src/Component/CMakeLists.txt
:
add_subdirectory(SubComponent1)
add_subdirectory(SubComponent2)
In this case, you will indeed need a CMakeLists file in src/Component/SubComponent{1,2}
declaring the sub libraries:
add_library(SubComponent1 SHARED src1.cpp)
# ...
target_link_libraries(MainProject SubComponent1)
Note that you can declare object libraries to separate compilation while avoiding multiple library files in the end:
add_library(SubComponent1 OBJECT src1.cpp)
# ...
add_library(MainProject $<TARGET_OBJECTS:SubComponent1> ...)
Upvotes: 1