Hi,
I recently got a task to move our codebase from using static builds to using shared libraries.
I was able to do the task and the code is building and working for the executables (bar some code I commented out because cyclic dependencies were removed).
One thing that bugged me was that I had to give a longer path for including headers inside a subdirectory. I refer any directory which doesn’t have its own CMakeLists.txt as a subdirectory here.
Basically lets assume the structure of the code is:
src/
→ dir1/ (has CMakeLists.txt)
→ → subdir1.1/
→ dir2/ (has CMakeLists.txt)
→ → dir3/ (has CMakeLists.txt)
→ → dir4/ (has CMakeLists.txt)
→ → → subdir4.1/
As you may guess, the directory code uses subdir code.
While the code was written as a Static library, I could do something like:
#include<subdir4.1/header.h>
while including a header from subdir4.1 in dir4.
With shared, I started getting errors that the file doesn’t exist.
I was able to fix the issue by giving the path in form:
#include<dir2/dir4/subdir4.1/header.h>
I’m using a GLOB_RECURSIVE to find .cpp and .h for add_library. Printing the variable showed that the subdir’s headers are included.
Looking at target_include_directories, I tried to add the subdirectory path, but that didn’t do anything. Again printing the include_dirs using get_target_property showed that the subdir path was getting appended.
I’ve tried building just dir4 (dir4 in this case is consumed by dir2) and I get the same error. Cleaning, rebuilding was tried, same for deleting CMakeCache.
Any suggestions or advice would be appreciated. Thanks
Thanks for the reply.
I’m already adding the lib to taret_link_library. I should have provided a proper code example. I’ll put it at the end of the comment.
> You will end in many include search paths!
I’m not sure what this means.
> I would use only src as include path.
This is what is happening right now. I’m afraid that by not allowing shorter paths, the other team members will complain. I know I found it annoying to do.
> I guess the subdirs maps your namespaces?
Not sure what you mean here. Subdirs may or may not have their own namespaces. Everything is based on what the dev making that code wanted to do at the time. No coding standards around namespaces defined.
Example CMakeLists.txt:
cmake_minimum_required (VERSION 3.10)
file(GLOB_RECURSE SRCS *.cpp *.h)
if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
add_compile_definitions(IS_WINDOWS_BUILD)
endif()
add_library(dir4 SHARED ${SRCS})
# Specify here the libraries this program depends on
target_link_libraries(dir4
core # lets assume a module core exists
git_version
${CMAKE_THREAD_LIBS_INIT}
)
# Specify here the include directories exported
# by this library
target_include_directories(dir4 PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/subdir4.1
)
dir2 consuming dir4 would be written as:
cmake_minimum_required (VERSION 3.10)
file(GLOB_RECURSE SRCS *.cpp *.h)
if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
add_compile_definitions(IS_WINDOWS_BUILD)
endif()
add_library(dir2 SHARED ${SRCS})
# Specify here the libraries this program depends on
target_link_libraries(dir2
dir4
core # lets assume a module core exists
git_version
${CMAKE_THREAD_LIBS_INIT}
)
# Specify here the include directories exported
# by this library
target_include_directories(dir2 PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)