I'm working on a new project, and trying to set up cmake from the ground up for the first time. I have two libraries that get built as targets in my project; libnet and liberror. I am linking libnet with liberror when I build the libnet target, and want to be able to #include header files that exist within liberror. I have the following added to the CMakeLists.txt for liberror:
target_include_directories(liberror SYSTEM PUBLIC core/error)
Now, I have the following added to the CMakeLists.txt for libnet:
target_link_libraries(libnet
PUBLIC
liberror)
So, liberror provides a header called ExceptionBuilder.hpp. When I try to do the following from libnet:
#include <core/error/ExceptionBuilder.hpp>
I get this error:
fatal error: core/error/ExceptionBuilder.hpp: No such file or directory
3 | #include <core/error/ExceptionBuilder.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I am quite new to cmake, so probably just setting something up incorrectly here, but any ideas? Here is the project structure:
Project Structure
Here is the verbose build output:
nmcurtis#DESKTOP-JC26375:~/distro/build$ make
-- Boost version: 1.58.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/nmcurtis/distro/build
Consolidate compiler generated dependencies of target distro_core_error_objects
[ 11%] Built target distro_core_error_objects
Consolidate compiler generated dependencies of target distro_core_utility_objects
[ 16%] Built target distro_core_utility_objects
[ 22%] Linking CXX shared library libdistro_core_utility.so
/usr/local/gcc-11.3.0/bin/g++-11.3 -fPIC -fPIC -Wno-terminate -shared -Wl,-soname,libdistro_core_utility.so -o libdistro_core_utility.so CMakeFiles/distro_core_utility_objects.dir/Concepts.cpp.o
[ 22%] Built target distro_core_utility
[ 27%] Linking CXX shared library libdistro_core_error.so
/usr/local/gcc-11.3.0/bin/g++-11.3 -fPIC -fPIC -Wno-terminate -shared -Wl,-soname,libdistro_core_error.so -o libdistro_core_error.so CMakeFiles/distro_core_error_objects.dir/Exception.cpp.o CMakeFiles/distro_core_error_objects.dir/ExceptionBuilder.cpp.o -Wl,-rpath,/home/nmcurtis/distro/build/core/utility: ../utility/libdistro_core_utility.so
[ 27%] Built target distro_core_error
[ 33%] Linking CXX static library libdistro_core_utility_static.a
/usr/bin/ar qc libdistro_core_utility_static.a CMakeFiles/distro_core_utility_objects.dir/Concepts.cpp.o
/usr/bin/ranlib libdistro_core_utility_static.a
[ 33%] Built target distro_core_utility_static
[ 38%] Linking CXX static library libdistro_core_error_static.a
/usr/bin/ar qc libdistro_core_error_static.a CMakeFiles/distro_core_error_objects.dir/Exception.cpp.o CMakeFiles/distro_core_error_objects.dir/ExceptionBuilder.cpp.o
/usr/bin/ranlib libdistro_core_error_static.a
[ 38%] Built target distro_core_error_static
Consolidate compiler generated dependencies of target distro_core_net_objects
[ 44%] Building CXX object core/net/CMakeFiles/distro_core_net_objects.dir/Address.cpp.o
/home/nmcurtis/distro/core/net/Address.cpp:4:10: fatal error: core/error/ExceptionBuilder.hpp: No such file or directory
4 | #include <core/error/ExceptionBuilder.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
core/net/CMakeFiles/distro_core_net_objects.dir/build.make:120: recipe for target 'core/net/CMakeFiles/distro_core_net_objects.dir/Address.cpp.o' failed
make[2]: *** [core/net/CMakeFiles/distro_core_net_objects.dir/Address.cpp.o] Error 1
CMakeFiles/Makefile2:262: recipe for target 'core/net/CMakeFiles/distro_core_net_objects.dir/all' failed
make[1]: *** [core/net/CMakeFiles/distro_core_net_objects.dir/all] Error 2
Makefile:135: recipe for target 'all' failed
make: *** [all] Error 2
nmcurtis#DESKTOP-JC26375:~/distro/build$
If you do target_include_directories(liberror SYSTEM PUBLIC core/error) you do not need core/error in #include <core/error/ExceptionBuilder.hpp>
Do this in core/error/CMakeLists.txt.
target_include_directories(liberror SYSTEM PUBLIC .)
# or
# target_include_directories(liberror SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
#include "ExceptionBuilder.hpp"
If you wish #include "core/error/ExceptionBuilder.hpp" then
do this in core/error/CMakeLists.txt.
target_include_directories(liberror SYSTEM PUBLIC ../..)
# or
# target_include_directories(liberror SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../..)
Related
I am using CMake to compile an application which uses the HSImage library on github. After installing with pip, the HSI library generates a shared library file, in my case it is created at /usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
I am trying to link this library to my application with CMake, but the CMake find_library method is having some trouble finding the library. Here is the relevant part of my CMakeLists.txt file:
CMakeLists.txt
set(HSI_DIR /usr/src/HSI)
find_library(HSI_LIB HSI.cpython-36m-aarch64-linux-gnu PATHS ${HSI_DIR})
message(STATUS "HSI: ${HSI_LIB}") # outputs /usr/src/HSI/HSI.cpython-36m-aarch64-linux-gnu.so
add_executable(${TARGET_NAME} <sources...>)
target_link_directories(${TARGET_NAME} PUBLIC ${HSI_DIR})
target_link_libraries(${TARGET_NAME}
${HSI_LIB}
<other libs...>
-Wl,--unresolved-symbols=ignore-in-shared-libs
)
When building, this produces the following error message:
cd /home/nvidia/projects/HsiInference/build;/usr/local/bin/cmake --build "/home/nvidia/projects/HsiInference/build" --target hsi_inference_onnx -- ;
Scanning dependencies of target hsi_inference_onnx
[ 14%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/targets/HsiInferenceOnnx/main_onnx.cpp.o
[ 28%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/targets/HsiInferenceOnnx/HsiInferenceOnnx.cpp.o
[ 42%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/ftpnano.cpp.o
[ 57%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/getOptions.cpp.o
[ 71%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/logger.cpp.o
[ 85%] Building CXX object CMakeFiles/hsi_inference_onnx.dir/src/utils.cpp.o
[100%] Linking CXX executable hsi_inference_onnx_debug
CMakeFiles/hsi_inference_onnx.dir/build.make:245: recipe for target 'hsi_inference_onnx_debug' failed
CMakeFiles/Makefile2:123: recipe for target 'CMakeFiles/hsi_inference_onnx.dir/all' failed
**/usr/bin/ld: cannot find -lHSI.cpython-36m-aarch64-linux-gnu**
**collect2: error: ld returned 1 exit status**
make[3]: *** [hsi_inference_onnx_debug] Error 1
make[2]: *** [CMakeFiles/hsi_inference_onnx.dir/all] Error 2
CMakeFiles/Makefile2:130: recipe for target 'CMakeFiles/hsi_inference_onnx.dir/rule' failed
make[1]: *** [CMakeFiles/hsi_inference_onnx.dir/rule] Error 2
Makefile:196: recipe for target 'hsi_inference_onnx' failed
make: *** [hsi_inference_onnx] Error 2
Build failed.
The important part:
/usr/bin/ld: cannot find -lHSI.cpython-36m-aarch64-linux-gnu
collect2: error: ld returned 1 exit status
From what I have gathered, target_link_libraries simply adds -l<library_name> to the link command, and -l<library_name> assumes that there is a file called lib<library_name>.so to link, which is not the case here. How can I get CMake to link the library properly despite the weird filename?
NOTE:
I am able to get the project to build by doing the following:
Delete the project's build directory to clear CMake caches
Rename the file or create a symbolic link to libhsi.so
Change CMakeLists.txt to find_library(HSI_LIB hsi PATHS ${HSI_DIR})
This changes the link command to -lhsi instead, which is able to find the renamed/soft-linked library file. HOWEVER, this is not ideal for me and the original question remains unanswered :)
For libraries with weird filename you should add : before the filename. Be careful, like mentioned in https://linux.die.net/man/1/ld : If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a (namespec is what comes after -l).
For your example you should replace ${HSI_LIB} in target_link_libraries by :${HSI_LIB}.so.
Trying to compile my project on Centos 8.2 using cmake 3.12, im facing with the following error:
cannot find -lpublic
here is full log:
[ 20%] Building CXX object CMakeFiles/ .cpp.o
[ 40%] Building CXX object CMakeFiles/ .cpp.o
[ 60%] Linking CXX shared library .so
[ 60%] Built target myproj
Scanning dependencies of target nist01
[ 80%] Building CXX object CMakeFiles/ .cpp.o
[100%] Linking CXX executable
/usr/bin/ld: cannot find -lpublic
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/nist01.dir/build.make:85: ...] Error 1
make[1]: *** [CMakeFiles/Makefile2:105: CMakeFiles/...] Error 2
make: *** [Makefile:84: all] Error 2
Tried to find something on the internet, but nothing shown up.
Thanks for your help.
That seems like you try to link against the target "public", which is a suspicious target name. It might be target_link_libraries command with lowercase public instead of uppercase one, like target_link_libraryies(nist01 public something) instead of target_link_libraries(nist01 PUBLIC something).
I am trying to build zephyr sample code that is meant for board A for board B (i.e. build was written for code A but I need to change it so that it works with board B). I keep coming across this error and I am not sure how to get past it. I have replaced the /board, /soc, and /dts subfolders with appropriate files for my board. I changed the Makefile to target my board and it is successfully finding it.
This is the error:
cmake -B build -DBOARD=my_project_name ./
-- Using out-of-tree SOC definition
-- Zephyr version: 2.2.0
-- Found PythonInterp: /opt/homebrew/bin/python3.6 (found suitable version "3.6.2", minimum required is "3.6")
-- git describe failed: fatal: Not a git repository (or any of the parent directories): .git;
BUILD_VERSION is left undefined
-- Selected BOARD my_project_name
-- Found west: /usr/local/bin/west (found suitable version "0.7.2", minimum required is "0.6.0")
-- Loading /Users/my_name/file_dir/Firmware/projects/my_project_name/boards/arm/my_project_name/my_project_name.dts as base
Devicetree header saved to '/Users/my_name/file_dir/Firmware/projects/my_project_name/build/zephyr/include/generated/devicetree_unfixed.h'
Parsing /Users/my_name/third-party/zephyr/zephyr_rtos/v2.2.0/zephyr/Kconfig
Loaded configuration '/Users/my_name/file_dir/Firmware/projects/my_project_name/boards/arm/my_project_name/my_project_name_defconfig'
Merged configuration '/Users/my_name/file_dir/Firmware/projects/my_project_name/prj.conf'
Configuration saved to '/Users/my_name/file_dir/Firmware/projects/my_project_name/build/zephyr/.config'
Kconfig header saved to '/Users/my_name/file_dir/Firmware/projects/my_project_name/build/zephyr/include/generated/autoconf.h'
-- The C compiler identification is GNU 8.2.1
-- The CXX compiler identification is GNU 8.2.1
-- The ASM compiler identification is GNU
-- Found assembler: /Users/my_name/file_dir/third-party/toolchains/gcc-arm-none-eabi-8-2018/arm-gcc-macos/bin/arm-none-eabi-gcc
-- Cache files will be written to: /Users/my_name/Library/Caches/zephyr
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/my_name/file_dir/Firmware/projects/my_project_name/build
make -C build
Scanning dependencies of target driver_validation_h_target
[ 1%] Generating include/generated/driver-validation.h
[ 1%] Built target driver_validation_h_target
Scanning dependencies of target kobj_types_h_target
[ 2%] Generating include/generated/kobj-types-enum.h, include/generated/otype-to-str.h
[ 2%] Built target kobj_types_h_target
Scanning dependencies of target syscall_list_h_target
[ 2%] Generating misc/generated/syscalls.json
[ 3%] Generating include/generated/syscall_dispatch.c, include/generated/syscall_list.h
[ 5%] Built target syscall_list_h_target
Scanning dependencies of target offsets
[ 6%] Building C object zephyr/CMakeFiles/offsets.dir/arch/arm/core/offsets/offsets.c.obj
In file included from /Users/my_name/file_dir/third-party/zephyr/zephyr_rtos/v2.2.0/zephyr/arch/arm/include/aarch32/cortex_m/stack.h:23,
from /Users/my_name/file_dir/third-party/zephyr/zephyr_rtos/v2.2.0/zephyr/arch/arm/include/kernel_arch_data.h:33,
from /Users/my_name/file_dir/third-party/zephyr/zephyr_rtos/v2.2.0/zephyr/arch/arm/core/offsets/offsets_aarch32.c:26,
from /Users/my_name/file_dir/third-party/zephyr/zephyr_rtos/v2.2.0/zephyr/arch/arm/core/offsets/offsets.c:12:
/Users/my_name/file_dir/third-party/zephyr/zephyr_rtos/v2.2.0/zephyr/include/arch/arm/aarch32/cortex_m/cmsis.h:92:2: error: #error "DT_NUM_IRQ_PRIO_BITS and __NVIC_PRIO_BITS are not set to the same value."
#error "DT_NUM_IRQ_PRIO_BITS and __NVIC_PRIO_BITS are not set to the same value."
^~~~~
make[3]: *** [zephyr/CMakeFiles/offsets.dir/arch/arm/core/offsets/offsets.c.obj] Error 1
make[2]: *** [zephyr/CMakeFiles/offsets.dir/all] Error 2
make[1]: *** [all] Error 2
make: *** [zephyr_project] Error 2
I have already checked to make sure that soc.h has #include <devicetree.h>. I'm not sure what else I can try now so any help is appreciated. Thanks!
Found the solution: I had to add dts_fixup.h to the bottom of my soc folder.
I've been trying to install a package called DAKOTA, which employs BOOST and Trilinos libraries which I'm already using in another program. For compiling and linking I'm using cmake, but I have not been able to pass the make step. It would be very helpful perhaps if you could point me out into some direction. Please find below the cmake command script I'm using to compile and link the libraries, and the process when it stops...
As far as I have understood is the following. I'm using Boost and Trilinos library files used to build the other program to built the project DAKOTA. But I get an error when creating the library file liboptpp.so from Teuchos_BLAS.cpp.o, because it-s being used by libteuchosnumerics.a. I'm not sure if I'm understanding it correctly, but what I got from this is that I need to create a shared library and recompile the Teuchos_BLAS.cpp.o again using -fPIC.
I tried including -fPIC in CMAKE_CXX_FLAGS as you may see below, and also -shared in CMAKE_SHARED_LINKER_FLAGS but this doesn't seem to work. In the past I also tried adding -fPIC in all flags from the CMakeCache.txt but also did not work.
Please let me know your understanding and suggestions.
Thank you very much in advance,
CMAKE COMMAND
! /bin/bash
cmake \
-D CMAKE_INSTALL_PREFIX=/home/pablo/BuildDakota/build \
-D MPI_BASE_DIR:PATH="/usr/lib/openmpi/" \
-D CMAKE_CXX_FLAGS:STRING="-O0 -std=c++11 -pedantic -ftrapv -Wall -fPIC -Wno-long-long" \
-D BOOST_ROOT:PATH=/home/pablo/BuildPeridigm/ \
-D Boost_NO_BOOST_CMAKE:BOOL=TRUE \
-D Boost_NO_SYSTEM_PATHS:BOOL=TRUE \
-D Trilinos_DIR:PATH=/home/pablo/BuildPeridigm/lib/cmake/Trilinos/ \
-D CMAKE_SHARED_LINKER_FLAGS:STRING="-shared" \
/home/pablo/BuildDakota/dakota-6.7.0.src
PROCESS
[ 9%] Building C object packages/external/OPTPP/src/CMakeFiles/optpp.dir/PDS/sort.c.o
[ 9%] Building C object packages/external/OPTPP/src/CMakeFiles/optpp.dir/PDS/writes.c.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/BoolVector.C.o
[ 10%] Building C object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/file_cutils.c.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/ioformat.C.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/mcholesky.C.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/OptppExceptions.C.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/OptppFatalError.C.o
[ 10%] Building CXX object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/print.C.o
[ 10%] Building C object packages/external/OPTPP/src/CMakeFiles/optpp.dir/Utils/timers.c.o
[ 10%] Linking CXX shared library ../lib/liboptpp.so
/usr/bin/ld: /home/pablo/BuildPeridigm/lib/libteuchosnumerics.a(Teuchos_BLAS.cpp.o): relocation R_X86_64_32S against `_ZN7Teuchos11ETranspCharE' can not be used when making a shared object; recompile with -fPIC
/home/pablo/BuildPeridigm/lib/libteuchosnumerics.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
packages/external/OPTPP/src/CMakeFiles/optpp.dir/build.make:2264: recipe for target 'packages/external/OPTPP/lib/liboptpp.so' failed
make[2]: *** [packages/external/OPTPP/lib/liboptpp.so] Error 1
CMakeFiles/Makefile2:1409: recipe for target 'packages/external/OPTPP/src/CMakeFiles/optpp.dir/all' failed
make[1]: *** [packages/external/OPTPP/src/CMakeFiles/optpp.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
I tried including -fPIC in CMAKE_CXX_FLAGS as you may see below, and
also -shared in CMAKE_SHARED_LINKER_FLAGS but this doesn't seem to
work.
Don't do this. Adding low-level compiler flags manually is almost always a bad idea in CMake.
The general approach to request a project to build as a dynamic library is to set the BUILD_SHARED_LIBS option of the project:
cmake -DBUILD_SHARED_LIBS=ON [...]
If this doesn't work right away, it usually means that the project was not intended to be built as a dynamic library, in which case there is no easy solution.
Dakota, for instance, claims to support this option properly.
If this doesn't work, you can try playing around with options like CMAKE_POSITION_INDEPENDENT_CODE, but your chances of success will not be very high. You should probably contact the library maintainer for advice in this case.
I want to use Googletest in my CLion Project.
So here is what I did:
I copied my googletest folder into the lib folder of my project.
I changed my CMakeLists.txt files like described in that Video: https://youtu.be/8Up5eNZ0FLw
and created that simple test:
#include "gtest/gtest.h"
TEST(basic_check, test_eq){
EXPECT_EQ(1,0);
}
I tried to run the test by pressing Shift + Ctrl + F10 and I got the following error:
"F:\Programs\CLion 2016.3\bin\cmake\bin\cmake.exe" --build C:\Users\Christian\ClionProjects\Geldautomat\cmake-build-debug --target runBasicTests -- -j 4
[ 16%] Building CXX object Tests/lib/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
In file included from C:/Users/Christian/ClionProjects/Geldautomat/Tests/lib/googletest/include/gtest/internal/gtest-internal.h:40:0,
from C:/Users/Christian/ClionProjects/Geldautomat/Tests/lib/googletest/include/gtest/gtest.h:58,
from C:\Users\Christian\ClionProjects\Geldautomat\Tests\lib\googletest\src\gtest-all.cc:39:
C:/Users/Christian/ClionProjects/Geldautomat/Tests/lib/googletest/include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::StrCaseCmp(const char*, const char*)':
C:/Users/Christian/ClionProjects/Geldautomat/Tests/lib/googletest/include/gtest/internal/gtest-port.h:2328:25: error: '_stricmp' was not declared in this scope
return _stricmp(s1, s2);
^
mingw32-make.exe[3]: *** [Tests/lib/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj] Error 1
mingw32-make.exe[2]: *** [Tests/lib/googletest/CMakeFiles/gtest.dir/all] Error 2
mingw32-make.exe[1]: *** [Tests/basic_tests/CMakeFiles/runBasicTests.dir/rule] Error 2
mingw32-make.exe: *** [runBasicTests] Error 2
Tests\lib\googletest\CMakeFiles\gtest.dir\build.make:62: recipe for target 'Tests/lib/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj' failed
CMakeFiles\Makefile2:162: recipe for target 'Tests/lib/googletest/CMakeFiles/gtest.dir/all' failed
CMakeFiles\Makefile2:267: recipe for target 'Tests/basic_tests/CMakeFiles/runBasicTests.dir/rule' failed
Makefile:200: recipe for target 'runBasicTests' failed
My CMakeLists.txt in this directory is:
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_executable(runBasicTests
basic_check.cpp)
target_link_libraries(runBasicTests gtest gtest_main)
target_link_libraries(runBasicTests src)
Where my Source Code can be found in directory src.
My main.cpp is in the root directory and my Basic Test is in Tests/basic_tests
Tried the same on my Linux Notebook with CLion. Got the Error:
/home/christian/Downloads/clion-2016.3/bin/cmake/bin/cmake --build /home/christian/ClionProjects/Geldautomat/cmake-build-debug --target runBasicTests -- -j 4
Scanning dependencies of target gtest
[ 16%] Building CXX object Tests/lib/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 33%] Linking CXX static library libgtest.a
[ 33%] Built target gtest
Scanning dependencies of target gtest_main
[ 50%] Building CXX object Tests/lib/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[ 66%] Linking CXX static library libgtest_main.a
[ 66%] Built target gtest_main
Scanning dependencies of target runBasicTests
[ 83%] Building CXX object Tests/basic_tests/CMakeFiles/runBasicTests.dir/basic_check.cpp.o
[100%] Linking CXX executable runBasicTests
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/bin/ld: cannot find -lsrc
collect2: error: ld returned 1 exit status
Tests/basic_tests/CMakeFiles/runBasicTests.dir/build.make:97: recipe for target 'Tests/basic_tests/runBasicTests' failed
gmake[3]: *** [Tests/basic_tests/runBasicTests] Error 1
CMakeFiles/Makefile2:256: recipe for target 'Tests/basic_tests/CMakeFiles/runBasicTests.dir/all' failed
gmake[2]: *** [Tests/basic_tests/CMakeFiles/runBasicTests.dir/all] Error 2
CMakeFiles/Makefile2:268: recipe for target 'Tests/basic_tests/CMakeFiles/runBasicTests.dir/rule' failed
gmake[1]: *** [Tests/basic_tests/CMakeFiles/runBasicTests.dir/rule] Error 2
Makefile:201: recipe for target 'runBasicTests' failed
gmake: *** [runBasicTests] Error 2
It would be fine if my Project runs on Linux..
Any suggestions?
-ChrizZ
You can follow this link ->
https://www.youtube.com/channel/UCvHU1FkIE-ogFTtXnKZsqWA
There are 2 videos. Both are helpful. I set up googleTest in my CLion yesterday following the 8:19 long video.