Dependencies between static library subdirectories and include paths - c++

I am working on a project that is to be made of one executable linking with two libraries.
I'm making frequent chages to all of them and don't wish to go though a sudo make install cycle for libA and libB, so I have laid them out in the following fashion:
myProject
├── libA
│   ├── CMakeLists.txt
│   ├── include
│ │   └── libA
│ │ └── *.h
│   └── src
│ └── *.cpp
│
├── libB
│   ├── CMakeLists.txt
│   ├── include
│ │   └── libB
│ │ └── *.h
│   └── src
│ └── *.cpp
│
├── CMakeLists.txt
├── include
│ └── *.h
└── src
└── *.cpp
I'm using CMake subdirectories to automatically link in libA and libB into myProject, and it works fine. The CMakeList.txts are set up as follows:
# myProject/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(myProject)
add_subdirectory(libA)
add_subdirectory(libB)
# Compiler flags, listing of the source files into ${SOURCE_FILES}, etc.
add_executable(myProject ${SOURCE_FILES})
target_link_libraries(myProject libA libB)
# myProject/libA/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(libA)
# Compiler flags, listing of the source files into ${SOURCE_FILES}, etc.
add_library(libA STATIC ${SOURCE_FILES})
target_include_directories(libA PUBLIC include)
install(
TARGETS libA
ARCHIVE DESTINATION lib
)
install(
DIRECTORY include/glk
DESTINATION include
)
myProject/libB/CMakeLists.txt is similar.
Now here's the issue: libB has a dependency on libA, and includes libA's headers through #include <libA/foo.h>, and will need symbols from libA upon linking (though I guess that's a non-issue with static libraries).
How can I set up this dependency correctly so that libB finds everything it needs about this local libA, ideally by modifying only myProject's configuration?

You need to specify a link dependency of libB on libA. Yes, they might be static libraries, but CMake allows you to specify that you link one library to another even if they are static. Where the libraries are static, such a link relationship is then only used for providing transitive dependencies between the targets (i.e. those provided as PUBLIC or INTERFACE items in the various target_... commands). So I'd expect your libB's CMakeLists.txt to look similar to libA', except you'd also add the following line:
target_link_library(libB PRIVATE libA)
Or if libB uses parts of libA in its own public API, make the above link dependency PUBLIC rather than PRIVATE. By doing this, anywhere that you have a target link against libB, CMake will ensure that libA appears after it on the final command line. In your example, the top level CMakeLists.txt file links libB after libA, so without the above target_link_library() relationship, the linker would complain about symbols from libA being missing, even though libA would have been on the linker command line, just in the wrong order. Your syntax for target_link_libraries() in your top level CMakeLists.txt files is also incorrect (presumably just a typo in your example). Arguments should not be comma-separated.
The other effect of explicitly linking libB to libA is that now libB picks up PUBLIC and INTERFACE properties from libA, which in this case will be the header search path that libA defines. You should find that libB will now find libA's headers too.

Related

How to setup CMake files and project structure for a C++ library used by others?

I'm writing a C++ library which has the following structure:
my_project
├── include
├── lib
├── src
│ ├── sourceA.cpp
│ └── sourceB.cpp
├── test
│ ├── test.cpp
│ └── CMakeLists.txt
└── CMakeLists.txt
Now I have multiple questions:
How can I layout CMake files in a way so that I can build a test app that uses it from within the library? Here's what I've got so far:
test/CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(supabase VERSION 0.2.0.0 LANGUAGES C CXX)
add_subdirectory(..)
add_executable(test test.cpp)
target_link_libraries(test PRIVATE mylib)
MakeLists.txt
add_library(mylib)
target_include_directories(mylib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include");
But CMake complains that
directory "C:/dev/myproject" is not a subdirectory of "C:/dev/myproject/test"
So I could of course create a top level CMakeLists.txt that contains the project information and builds test.cpp. BUT: Of course don't want other developers to build the test routines when they just want to build the lib. Which leads me to question number..
Is there a standardised way to layout the project structure so that it satisfies the following requirements:
I can build testscripts from within library and use it at the same time
Other devs can just include my top level CMakeLists.txt and build the library without baggage.
What is best practice here?

Which libraries are available to link in cmake?

I was learning CMake for a project and feel confused when linking libraries.
I find it easier to ask with an example, as using terms I am not familiar could be misleading.
The questions are (also commented in the example codes)
how can I know what are the library name I can link to my target? Can I tell that from the build file structure, or I need to look into CMakeLists.txt of the external libraries. (e.g. the gtest-main and libhermes in the example)
How can I use a "grandchild" library that is part of the external library? (e.g. the jsi library in the example)
Example
Let's say I am writing some tests with dependency on the Hermes Library. I then write a CMakeList.txt file like
cmake_minimum_required(VERSION 3.13)
project(mytests_cxx)
set(CMAKE_CXX_STANDARD 14)
include(FetchContent)
FetchContent_Declare(
hermes
URL https://github.com/facebook/hermes/archive/d63feeb46d26fe0ca7e789fc793f409e5158b27f.zip
)
FetchContent_MakeAvailable(hermes)
enable_testing()
add_executable(
hello_test
./test.cpp
)
find_library (
LIB_HERMES
hermes # <- how can I know what is the name?
)
find_library (
LIB_JSI
jsi # <- How could I use a "grandchild" dependency, it is defined here (https://github.com/facebook/hermes/tree/main/API/jsi/jsi)
)
target_link_libraries(
hello_test
gtest_main <- Hermes seems to use gtest, how could make sure gtest_main is available?
LIB_HERMES
LIB_JSI
)
include(GoogleTest)
gtest_discover_tests(hello_test)
Running cmake to build
cmake -S . -B build -DHERMES_BUILD_SHARED_JSI=ON // I see an option defined in heremes'CMakeLists.txt but not sure if I am using it right.
the directory in build
tree ./build -L 2
./build
├── CMakeCache.txt
├── CMakeFiles
│ ├── 3.23.1
│ ├── CMakeDirectoryInformation.cmake
│ ├── CMakeError.log
│ ├── CMakeOutput.log
│ ├── CMakeRuleHashes.txt
│ ├── CMakeTmp
│ ├── Makefile.cmake
│ ├── Makefile2
│ ├── Progress
│ ├── TargetDirectories.txt
│ ├── cmake.check_cache
│ ├── hello_test.dir
│ └── progress.marks
├── CTestTestfile.cmake
├── ImportHermesc.cmake
├── Makefile
├── _deps
│ ├── hermes-build
│ ├── hermes-src
│ └── hermes-subbuild
├── bin
│ └── hermes-lit
├── cmake_install.cmake
├── compile_commands.json
└── hello_test[1]_include.cmake
then I run cmake --build build and see this error
/.../test.cpp:8:10: fatal error: 'jsi/jsi.h' file not found
#include <jsi/jsi.h>
Your question is too broad. Either ask a question about the list of libraries in cmake, or ask a question about how to compile and link with hermes, or ask a question about a specific problem with your build about including a third party. These should be 3 separate questions on this site.
how can I know what are the library name I can link to my target?
There are two things that you can target_link_libraries - you can link with libraries available on your system and link with targets defined in cmake.
You can list libraries available on your system by traversing all paths outputted by ld --verbose and listing all lib*.so files in those directories. Or https://unix.stackexchange.com/questions/43187/how-do-i-get-a-list-of-shared-library-filenames-under-linux or similar. See man ld.so.
You can list all targets defined in cmake. How do I list the defined make targets from the command line? .
These lists combined consist of all possible libraries names that you can link with.
Can I tell that from the build file structure
I do not understand. No.
I need to look into CMakeLists.txt of the external libraries
Yes, usually you want to look into any external libraries CMakeLists.txt at least to check for malicious code.
You usually look into documentation of external libraries. And, because source code is a form of documentation in open-source projects, you would look into the source code, yes.
How can I use a "grandchild" library that is part of the external library?
There are multiple ways for including external projects. Cases were outlined in Correct way to use third-party libraries in cmake project .
That said, not all CMake projects work properly with add_subdirectory() (which is called by FetchContent_MakeAvailable) - there are many cases where the third-party projects CMakeLists.txt may not work with parent project. In such cases, I would go with compiling and installing the project into a subdirectory for example before running CMake or with ExternalProject_Add, and then only including the result of compilation with hand-crafted find_library and target_include_directories from the installation directory.
Overall, including a third party is not always an easy task, and yes it requires you to become intimate with the specifics of a third party project and how it works and how it was written and decide how you want it to include in your project.

Integrate thirdpary shared libraries(*.so) into multiple projects using cmake

I have the following layout which contains multiple projects separate directories (It's a CPP project):
Demo
├── Lib
│   ├── CMakeLists.txt
│   ├── inc
│   └── lib
│   ├── crypto_ic.so
│   └── crypto.so
├── Proj1
│   ├── App1
│   └── CMakeLists.txt
└── Proj2
├── App2
└── CMakeLists.txt
I created one separate third-party-lib.bb file to install crypto_ic.so,crypto.so into /usr/lib/ in the target using do_install_append.
All applications are depending on third-party libraries. There are no parent cmake for proj1,proj2 those are independent projects built by *.bb files using yocto.
Now I need to integrate the third party library for all the applications. I created one interface library for the third party headers and using find_package I am able to find the headers and the compilation is working fine.
CMakeLists.txt(Lib)
cmake_minimum_required(VERSION 3.8)
project(crypto VERSION 1.0.0 LANGUAGES CXX)
include(GNUInstallDirs)
find_package(Boost REQUIRED)
add_library(crypto INTERFACE)
target_link_libraries(crypto INTERFACE
Boost::boost
)
target_include_directories(crypto INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/inc/>
$<INSTALL_INTERFACE:include>
)
# Create and Install *-config.cmake,*-version.cmake files
# Install exported targets, i.e. *-targets.cmake
# Install header files
CMakeLists.txt(Proj1)
cmake_minimum_required(VERSION 3.8)
project(App1 VERSION 1.0.0 LANGUAGES CXX)
include(GNUInstallDirs)
find_package(crypto REQUIRED)
add_executable(run Run.cpp)
target_link_libraries(run PUBLIC
crypto
)
But the problem is while linking I am getting a linker error.
Run.cpp: undefined reference to 'Intialize(std::string hashName)'
Initialize function definition is available in the shared library.
Any thoughts on how to fix the issue and how to remove the hardcode path to the library in CMake?.
I am new to CMake and not sure this is a good approach to follow, only reason I created the Interface library is to install the headers in target and find the package. I can modify it if there are some better solutions?
Edit:
I found the issue in the cmake, I didn't link the shared library files (*.so) in the application that's the reason I am getting the linker error. But I am not getting how to link that from the target?

verifying my understanding of a CMakeLists.txt file

I successfully played trial-and-error with a CMakeLists.txt file, and now everything builds. However, I want to make sure my understanding is correct about how it's working.
I have a project with the following structure:
./
├── bin
├── CMakeLists.txt
├── include
│   └── sql-writer
│   ├── credentials.h
│   ├── database.h
│   ├── datetime.h
│   ├── market_bar.h
│   └── writer.h
├── README.md
├── src
│   ├── CMakeLists.txt
│   ├── database.cpp
│   ├── main.cpp
│   └── writer.cpp
└── test
├── CMakeLists.txt
├── test-main.cpp
├── test_sym_table_pairs
└── writer_tests.cpp
This builds an executable, a static library, and a test executable. The problem was src/CMakeLists.txt:
set(HEADER_DIR ${sql-writer_SOURCE_DIR}/include/sql-writer)
add_executable(sql-writer-demo main.cpp ${HEADER_DIR})
target_include_directories(sql-writer-demo PUBLIC ${HEADER_DIR}) #!
target_link_libraries(sql-writer-demo sql-writer-lib mysqlcppconn ${Boost_LIBRARIES})
file(GLOB SOURCES ${sql-writer_SOURCE_DIR}/src/*.cpp)
file(GLOB HEADERS ${HEADER_DIR}/*.h)
add_library(sql-writer-lib ${HEADERS} ${SOURCES})
target_link_libraries(sql-writer-lib mysqlcppconn ${Boost_LIBRARIES})
target_include_directories(sql-writer-lib PUBLIC ${HEADER_DIR})
the first line's set command just defines HEADER_DIR. This is easy.
target_include_directories(sql-writer-demo PUBLIC ${HEADER_DIR}) must be included after the first add_execuable because otherwise header files will need to be included with relative paths. Is that right? Why does it come after? I've heard cmake treats include directories as "separate" from "sources" (cmake sources not .cpp files). What does that mean?
target_link_libraries(sql-writer-demo sql-writer-lib mysqlcppconn ${Boost_LIBRARIES}) needs to come after add_executable(sql-writer-demo main.cpp ${HEADER_DIR}) as well, and it tells how to link to other libraries.
file(GLOB SOURCES ${sql-writer_SOURCE_DIR}/src/*.cpp) is not recomended before only because the directory/file structure could change, and then this wouldn't work anymore
target_link_libraries(sql-writer-lib mysqlcppconn ${Boost_LIBRARIES}) needs to come after add_library(sql-writer-lib ${HEADERS} ${SOURCES}) because the compulation of my library sql-writer-lib needs to use other libs, and this is where we tell cmake to look out for those.
You typically don't include headers into add_executable and add_library, but only implementation files:
add_executable(sql-writer-demo main.cpp)
add_library(sql-writer-lib ${SOURCES})
add_executable and add_library create a target, target_include_directories specifies include directories for this target. The documentation is pretty clear:
The named <target> must have been created by a command such as add_executable() or add_library() and must not be an IMPORTED target.
If you use target_include_directories(sql-writer-lib PUBLIC ${HEADER_DIR}) with PUBLIC specifier, you don't need to use target_include_directories to specify include directories for targets that link against that library, because the dependency will be propagated automatically. The same applies to target_link_libraries.
So, src/CMakeLists.txt
file(GLOB SOURCES ${sql-writer_SOURCE_DIR}/src/*.cpp)
add_library(sql-writer-lib ${SOURCES})
target_link_libraries(sql-writer-lib PUBLIC mysqlcppconn ${Boost_LIBRARIES})
target_include_directories(sql-writer-lib PUBLIC ${HEADER_DIR})
add_executable(sql-writer-demo main.cpp)
target_link_libraries(sql-writer-demo sql-writer-lib)
should work. Whether you want to use PUBLIC on mysqlcppconn ${Boost_LIBRARIES} depends on the structure of your project. I specified it as an example only.

CMake library dependencies

This may be a tad hard to explain in full. I have setup a directory structure for a series of C++ libraries I am writing. I intend to use CMake to handle building these libraries. For the most part these libraries are completely separate "subsystems" but in the odd places one library needs access to the header file contained within another. Here is my directory structure.
base
├─ cmake
├─ docs
├─ examples
└─ sources
├─ libA
│ ├─ include
│ │ └─ libA
│ │ ├─ core.h
│ │ └─ *.h
│ ├─ source
│ │ └─*.cpp
└─ libB
├─ include
│ └─ libB
│ ├─ message.h
│ └─ *.h
└─ source
└─ *.cpp
There are CMakeLists.txt files in a few places. The main one is within the root directory base which sets up a bunch of variables and options and lastly calls add_subdirectory(sources). Within sources the CMakeLists.txt file simply calls:
add_subdirectory(libA)
add_subdirectory(libB)
Lastly there are CMakeLists.txt files in both the libA and libB directories. These are both largely the same except for the target name. An example of one of these files is:
set(target libA)
# Sources
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
# Add include directories
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
# Set the source files to compile
file(GLOB_RECURSE sources ${source_path}/**/*.cpp)
# Build library
add_library(${target} ${sources})
Now separately both of these libraries can be built without issue but I need to be able to include a header from libA in a source file within libB
// base/sources/libB/source/message.cpp
#include <ctime>
#include <iostream>
#include <libA/core.h>
#include <libB/message.h>
However when I build this I get the following error:
fatal error: 'libA/core.h' file not found
I have tried using target_link_libraries and target_include_directories without any luck. Clearly I am using these wrong or simply don't understand what each of these do.
Since your library libB needs a header from libA to compile, let's say libA is a dependency of libB. Fortunately, CMake is very clever at handling dependencies, so your job is pretty easy:
libA
Instead of
# Add include directories
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include")
add to the end
target_include_directories(${target} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
This will tell CMake that it must also include the headers for libA to the other projects that need it.
libB
Just tell CMake that libB needs libA:
target_link_libraries(${target} libA)
Note that this doesn't relate directly to the linking step when you compile. For libraries it mainly tells that there is this dependency, and in this case it will automatically add the paths for the headers automatically.
You can add to your CMakeLists.txt for libB, another include directory:
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../libA/include")