Including my libraries without a full path - c++

The project has the following structure:
.
|- CMakeList.txt
`- src
|- lib
| |- CMakeList.txt
| `- libA
| |- CMakeList.txt
| |- libA.c
| `- libA.h
`- main
|- CMakeList.txt
`- main.c
and I want to include the libA.h file in main.c using #include "libA.h", but an error occurs when trying to compile fatal error: hello.h: No such file or directory. Can I make cmake add the appropriate include flags and I could write #include "libA.h".
I understand that I can manually add flags like -Ipath/to/src/lib/libA -Ipath/to/src/lib/libB ..., but I would like to automate this process.

Include_directories should solve that for you:
https://cmake.org/cmake/help/v3.0/command/include_directories.html

You can use the built-in PROJECT_SOURCE_DIR CMake variable to access the root source directory of your project. Use this with target_include_directories() to use #include "libA.h" in main.c:
In your src/main/CMakeLists.txt:
add_executable(MyMain main.c)
target_include_directories(MyMain PUBLIC ${PROJECT_SOURCE_DIR}/src/lib/libA)

You want to set your target_include_directories in your libA/CMakeLists.txt. After the target is built with add_library, you want to add the following instruction:
add_library(libA libA.c libA.h)
target_include_directories(libA PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
This instruction will specify that libA and any target that links to it also considers ${CMAKE_CURRENT_SOURCE_DIR} as a path to look for includes. This is known as a library's public header. Ideally, you would want those header files to be in their own subdirectory so you could include them easily to your other projects, without having the libA.c file be in the include directory. For example:
.
|- CMakeList.txt
`- src
|- lib
| |- CMakeList.txt
| `- libA
| |- CMakeList.txt
| |- include
| | `-libA.h
| |- src
| `- libA.c
`- main
|- CMakeList.txt
`- main.c

Solved.
If I create library
add_library(MyLib src/MyLib.cpp include/MyLib.h)
target_include_directories(MyLib PUBLIC include)
then I can write
add_executable(MyExe ${SOURCES} ${HEADERS})
target_link_libraries(MyExe MyLib)
The last line will automatically add the flag '-I/path/to/MyLib/include' when compiling MyExe.

Related

Include particular header files in a cmake project with absolute paths

to start with, I have the following repo structure.
root
|
|--- blocks
| |
| |---example
| |
| |---example.cpp
| |---CMakeLists.txt
|
|--- interfaces
|
|--- types
|
|-- types1.h
|-- types2.h
|-- CMakeLists.txt
What I would like to do is to only include types1.h in the cmake target of example.cpp. And I would like to include types1.h such that I can include it in example.cpp using its path relative to root (i.e. #include "interfaces/types/types1.h") and I don't want to use include_directories(${CMAKE_SOURCE_DIR}) since that would expose the whole repo to the target.
target_include_directories did not work since it only includes directories but not individual files and I don't want to include a whole directory and expose irrelevant headers in types folder to example.cpp or move types1.h to a dedicated subfolder.
Do you have any suggestions on how to achieve this?
My current solution is as below which gives fatal error: interfaces/types/types1.h: No such file or directory when I compile exampleblock target. (I eased the requirements for start and allowed interface_types target to include types2.h also even though example.cpp doesn't need it)
blocks/example/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
add_library(exampleblock STATIC
example.cpp
)
target_link_libraries(exampleblock INTERFACE
${CMAKE_SOURCE_DIR}/interfaces/types:interface_types
)
interfaces/types/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
add_library(interface_types INTERFACE)
target_include_directories(interface_types INTERFACE .)

Dependent sibling subdirectories in CMake

My current project has the following structure:
root
|- parser
| |- include // a directory for headers
| |- src // a directory for sources
| |- parser.yy
| |- scanner.ll
| |- CmakeLists.txt
|
|- preprocessor
| |- include // a directory for headers
| |- src // a directory for sources
| |- CmakeLists.txt
|
|- main.cpp
|- CMakeLists.txt
Now I want to start generating assembly codes. I have to use staticstack and codegen (are classes which are going to be defined) in the parser.yy and in srcfolder of the parser foler.
I cannot figure out how to do that.
What's the best practice in CMake to do this ?
Should I make subdirectories named semstack and codegen in parser folder ? This doesn't seem correct to me since as a CMake project gets larger the hierarchy will get deeper and if more dependencies are there then it would be a mess.
Should I make these subdirectories in the root ? If then what is the syntax in CMake to use sibling subdirectory ?
If then what is the syntax in CMake to use sibling subdirectory ?
There is no special syntax for this since normal (i.e. not IMPORTED) targets are global. If you define a library in one subdirectory, it may be used in any other via target_link_libraries.
For instance:
# parser/CMakeLists.txt
add_library(parser ...)
add_library(proj::parser ALIAS parser)
target_include_directories(
parser PRIVATE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>")
target_link_libraries(
parser PRIVATE proj::semstack proj::codegen)
The code for other subdirectories is similar.
# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(proj)
add_subdirectory(semstack)
add_subdirectory(codegen)
add_subdirectory(parser)
add_subdirectory(preprocessor)
add_executable(main main.cpp)
target_link_libraries(
main PRIVATE proj::parser proj::preprocessor)
I create and link to ALIAS targets to avoid a typo in a target name being forwarded to the linker verbatim (names with :: in CMake are always considered targets and this is validated at generation time).

How to write a CMakeLists.txt to build some source code and its sample code?

There are several C++ source codes which don't utilize CMake as a build system. Suppose I have such a file structure:
ProjectRepoDir
|- include
|- liba.h
|- module1.h
|- src
|- main.cpp
|- liba.cpp
|- module1.cpp
|- samples
|- example1-dir
|- main.cpp
|- example2-dir
|- main.cpp
Can I create a CMakeLists.txt under the ProjectRepoDir, and in the directory I do these commands to build the source code and all the samples directories? The reason is that I don't want to write CMakeLists.txt in each samples directory.
mkdir build && cd build
cmake ..
make
Sure, you can do everything from the top-level CMakeLists.txt:
# Extract the common parts in a (internal) static library
add_library(liba STATIC src/liba.cpp src/module1.cpp)
target_include_directories(liba PUBLIC include)
add_executable(my-project src/main.cpp)
target_link_libraries(my-project liba)
# Add a `samples` target that enables building the sample programs
# Not built by default.
add_executable(sample1 EXCLUDE_FROM_ALL samples/example1-dir/main.cpp)
target_link_libraries(sample1 liba)
add_executable(sample2 EXCLUDE_FROM_ALL samples/example2-dir/main.cpp)
target_link_libraries(sample2 liba)
add_custom_target(samples DEPENDS sample1 sample2)

CMake: Build and install locally stored submodule

There are numerous similar questions on Stack Overflow, but none come near to answering my question.
I have a C++ library built using CMake:
mylib
| - CMakeLists.txt
| - src/
| | - m.h
| | - m.cpp
| - include/
| | - mylib/
| | | - a.h
| | | - something/
| | | | - some.h
| - cmake/
| - mylibConfig.cmake.in
| - mylibConfigVersion.cmake.in
I then create another library or executable which includes the aforementioned library:
myapp
| - CMakeLists.txt
| - src/
| | - main.cpp
| - include/
| | - myapp/
| | | - b.h
| - libs
| | - mylib
And would like to use mylib within myapp like so. Please take notice how mylib headers are included in a directory like format:
#include <mylib/a.h>
#include <mylib/something/some.h>
mylib should be built when building myapp so that the following works without any other build steps:
$ cd myapp/build
$ cmake ..
$ make
Here is a list of some of the Stack Overflow posts I have reviewed. I have attempted each, and they simply do not work:
CMake: How to build external projects and include their targets
CMake ExternalProject_Add() and FindPackage()
Assuming in the project described, only header files in the mylib/include directory are to be considered public:
The top-level CMakeLists.txt for mylib needs to contain:
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(mylib VERSION 0.1 LANGUAGES CXX)
add_library(${MyLibName} ${MyLib_SOURCES})
target_include_directories(${MyLibName}
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
This ensures that projects including mylib will only have access to files in the include/ directory. Files in include/ can be used like #include <myfile.h> and files in include/mylib (the general convention) can be used like #include <mylib/myfile.h>.
The top-level CMakeLists.txt for myapp should include:
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(myapp VERSION 0.1 LANGUAGES CXX)
add_subdirectory(libs/mylib)
add_executable(${MyAppName} ${MyApp_SOURCES})
target_link_libraries(${MyAppName} ${MyLibName}
The use of add_subdirectory ensures that mylib will be built before myapp and target_link_libraries adds mylib to the executable.
As mentioned by Tzalumen, make sure you take a look at the CMake tutorials and prefer the use of cmake --build . instead of make.
So, I actually do this in one of my projects.
First, the library needs to be built with ADD_LIBRARY(), but I'm assuming you do that. Something like
ADD_LIBRARY (${MyLibName} ${SOURCES}) in the library's CMakeLists.txt
That adds the library into cmake's list of libraries.
Second, you add the library to your project with
target_link_libraries (${PROJECT_NAME} ${MyLibName}) in the executable's CMakeLists.txt, the same one where you have your add_executable (${PROJECT_NAME} ${SOURCES})
That will set up the dependency chain to force MyLibName to explicitly build before PROJECT_NAME.

how to get multiple libraries from the same project in cmake in my project

I have a project like this:
main
|- CMakeLists.txt
|- src
|- api_1
| |- CMakeLists.txt (#1)
| |- api_1.h
| |- api_1.cpp
|
|- api_2
|- CMakeLists.txt (#2)
|- api_2.h
|- api_2.cpp
When I compile the probject, I would like to obtain the following objects
as a result:
executable
libapi_1.so / libapi_1.a
libapi_2.so / libapi_2.a
The project currently builds libapi_1.so/libapi_1.a, but it does not build the other two. Due to the very high complexity of the probject and obscure naming, I am unable to understand where / how appropriately modify the main-level CMakeLists.txt in order to get an additional .so / .a file.
Can someone provide me with clear instructions on what is needed?
You can setup 2 (or more) projects in your CMakeLists.txt:
project(FullProject)
# config...
add_executable(exec1 ${sources_files}) # This line will create a new executable target
target_link_libraries(exec1 lib_you_need)
# config exec1...
# Now defines another target which is a library
add_library(lib1 ${sources_files}) # This line will create a new library target
target_include_directories(lib1 PRIVATE path_you_need)
When you'll build, you'll have 2 target: exec1 and lib1 and you can build the one you want using:
cmake --build . --target your_target # exec1 or lib1
or
make your_target # exec1 or lib1