I am working on a project were I maintain a CMakeLists.txt file to keep track of the dependencies when I need to compile.
I recently, started using a new library that I need to integrate in my project. The library comes with some examples on how to compile but this is purely a Makefile. I would like to integrate the logic of the Makefile into my project's CMakeLists.txt file.
The Makefile of this library is as follows:
COMMON=-O2 -I/home/john/.mujoco/mjpro200/include -L/home/john/.mujoco/mjpro200/bin -std=c++11 -mavx -pthread -Wl,-rpath,'$$ORIGIN'
all:
g++ $(COMMON) basic.cpp -lmujoco200 -lGL -lglew /home/john/.mujoco/mjpro200/bin/libglfw.so.3 -o basic
gcc -c -O2 -mavx -I/home/john/.mujoco/mjpro200/include /home/john/.mujoco/mjpro200/include/uitools.c
rm *.o
I would like to convert this Makefile into CMakeLists.txt exactly if possible. I see that they provide some flags for threading (pthread) and I want to keep these flags and settings in my CMakeLists.txt in case they are needed for performance.
Here is my CMakeLists.txt:
cmake_minimum_required (VERSION 2.6.0)
project(myproject)
add_compile_options(-std=c++11) # CMake 2.8.12 or newer
if( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )
add_definitions("-fno-strict-aliasing -Wall")
endif( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )
# optional in case boost is used
find_package(Boost 1.58 EXACT)
find_package(Eigen REQUIRED)
set(mujocoCommon "-O2 -I/home/john/.mujoco/mjpro200/include -L/home/john/.mujoco/mjpro200/bin -std=c++11 -mavx -pthread -Wl,-rpath,'$$ORIGIN'")
set(Mujoco_INCLUDE_DIRS "/home/john/.mujoco/mjpro200/include")
include_directories(${Boost_INCLUDE_DIRS} ${Eigen_INCLUDE_DIRS} ${Mujoco_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
add_executable(myproject mycode.cpp)
install(TARGETS myproject DESTINATION .)
So I tried to define a variable mujocoCommon similar to the COMMON in the Makefile as well as the Mujoco_INCLUDE_DIRS that I then use in the CMake's include_directories.
Note that in the myproject.cpp I have MuJoCo code and other dependencies that I need to correctly link against. In the Makefile things looks a bit more simpler because in there the code has only MuJoco dependencies.
I need some heads-up on how to add in my CMakeLists.txt the following:
-mavx -pthread -Wl,-rpath,'$$ORIGIN' part of Makefile's COMMON variable.
-lmujoco200 -lGL -lglew /home/john/.mujoco/mjpro200/bin/libglfw.so.3
-c -O2 -mavx -I/home/john/.mujoco/mjpro200/include /home/john/.mujoco/mjpro200/include/uitools.c
Currently with the minimal knowledge on CMake and what I have in my CMakeLists.txt when I compile using "make" I get many errors of the kind:
myproject.cpp:(.text+0xfa9): undefined reference to `glfwSetMouseButtonCallback'
Basically I would like to link (?) my code with MuJoco library as is done in their Makefile.
Thanks.
Edit
In regards with the possible duplicate flag, I added the following flag:
set(Mujoco_LIBRARIES "/home/john/.mujoco/mjpro200/bin")
target_link_libraries(myproject ${Mujoco_LIBRARIES} -lboost_system)
But it didn't solved the problem.
Edit 2:
I have also added this one:
set(CMAKE_CXX_FLAGS "-lmujoco200 -lGL -lglew /home/john/.mujoco/mjpro200/bin/libglfw.so.3")
The errors I am getting:
/usr/bin/x86_64-linux-gnu-ld: cannot find -lmujoco200
/usr/bin/x86_64-linux-gnu-ld: cannot find -lglew
You also need to add the opengl libraries to the link libraries, target_link_libraries. Don't add them with -l.
Related
I am trying to write a proper CMake script for building an embedded project(STM32). Our project structure looks like this:
App/
....Module1/
....Module2/
Core/
....OwnFramework/
........CoreModule1/
........CoreModule2/
....CubeMXGeneratedCode/
........Core/ (there are more files and dirs, but they are not relevant)
...........Src/main.c (only important info is that main is here)
........Drivers/
....Libs/
........Lib1/ (for example ETL)
Right now there is one CMakeLists.txt file in root project dir, but this seems like a bad solution. I've tried adding CMakeLists into App/ and App/Module but then I would get linking errors (while looking at compile_commands I've seen that they were compiled with almost no flags).
./CMakeLists.txt (removed some non-important parts, like set variables that are obvious, so it's little bit shorter)
set(APP_INCLUDE_DIRECTORIES
${APP_DIR}/Module1
${APP_DIR}/Module2
)
set(CORE_INCLUDE_DIRECTORIES
${Core_DIR}/CoreModule1
${Core_DIR}/CoreModule2
)
set(FLAGS
"-Wl,--gc-sections")
set(CPP_FLAGS
"-fno-rtti -fno-exceptions -fno-threadsafe-statics")
set(CPU_PARAMETERS
-mcpu=cortex-m4
-mthumb
-mfpu=fpv4-sp-d16
-mfloat-abi=hard)
set(STARTUP_SCRIPT ${CUBEMX_DIR}/startup_stm32fxxxxx.s)
set(MCU_LINKER_SCRIPT ${CUBEMX_DIR}/STM32Fxxxxxxx_FLASH.ld)
set(CUBEMX_INCLUDE_DIRECTORIES
${CUBEMX_DIR}/Core/Inc
... and some more Inc dirs
set(PROJECT_INCLUDE_DIRECTORIES
${CMAKE_SOURCE_DIR}
${APP_INCLUDE_DIRECTORIES}
${CORE_INCLUDE_DIRECTORIES})
file(GLOB_RECURSE STM32CUBEMX_SOURCES
${CUBEMX_DIR}/Core/*.c
${CUBEMX_DIR}/Drivers/*.c
${CUBEMX_DIR}/Middlewares/Third_Party/FreeRTOS/Source/*.c)
file(GLOB_RECURSE PROJECT_SOURCES FOLLOW_SYMLINKS
${APP_DIR}/*.cpp
${APP_DIR}/*.c
${CORE_DIR}/*.cpp
${CORE_DIR}/*.c)
add_executable(${EXECUTABLE}
${STM32CUBEMX_SOURCES}
${PROJECT_SOURCES}
${STARTUP_SCRIPT})
target_compile_definitions(${EXECUTABLE} PRIVATE
${MCU_MODEL}
USE_HAL_DRIVER)
target_include_directories(${EXECUTABLE} PRIVATE
${CUBEMX_INCLUDE_DIRECTORIES}
${PROJECT_INCLUDE_DIRECTORIES})
target_compile_options(${EXECUTABLE} PRIVATE
${CPU_PARAMETERS}
${FLAGS}
-Wall
-Wextra
-Wpedantic
-Wno-unused-parameter
$<$<COMPILE_LANGUAGE:CXX>:
${CPP_FLAGS}
-Wno-volatile
-Wold-style-cast
-Wuseless-cast
-Wsuggest-override>
$<$<CONFIG:Debug>:-Og -g3 -ggdb>
$<$<CONFIG:Release>:-Og -g0>)
target_link_options(${EXECUTABLE} PRIVATE
-T${MCU_LINKER_SCRIPT}
${CPU_PARAMETERS}
-Wl,-Map=${CMAKE_PROJECT_NAME}.map
--specs=nosys.specs
-Wl,--start-group
-lc
-lm
-lstdc++
-lsupc++
-Wl,--end-group
-Wl,--print-memory-usage)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD
COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${EXECUTABLE}>)
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${EXECUTABLE}>
${EXECUTABLE}.hex
COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${EXECUTABLE}>
${EXECUTABLE}.bin)
There is also toolchain file:
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(TOOLCHAIN_PREFIX arm-none-eabi-)
set(FLAGS
"-fdata-sections -ffunction-sections \
--specs=nano.specs -Wl,--gc-sections")
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc ${FLAGS})
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++ ${FLAGS})
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_SIZE ${TOOLCHAIN_PREFIX}size)
set(CMAKE_EXECUTABLE_SUFFIX_ASM ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_C ".elf")
set(CMAKE_EXECUTABLE_SUFFIX_CXX ".elf")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
I am building everything with this command:
cmake -B${workspaceFolder}/build -DCMAKE_TOOLCHAIN_FILE=gcc-arm-none-eabi.cmake -DCMAKE_BUILD_TYPE=Debug
And this setup works, but I have a feeling that this is a vary bad way of doing it. When I tried adding subdirectories in App and each module, I've got linking erros due to problems with compile flags (arm none eabi elf uses VFP register arguments linking error).
I am also not sure how well will it work when I start adding libs like Embedded Template Library.
So my question is, how do I add subdirectories so they compile the same way my root CMakeLists does? I've tried doing this following multiple guides and examples like this one: https://github.com/rgujju/STM32_Base_Project/blob/master/modules/simple_module/CMakeLists.txt.
But when I've put this in my App/Module/CMakeLists.txt
set(MODULE_NAME simple_module)
file(GLOB SOURCES ./*.c)
add_library(${MODULE_NAME} STATIC
${SOURCES}
)
target_include_directories(${MODULE_NAME}
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${PROJECT_SOURCE_DIR}/include
)
And added add_subdirectory(App) to main, my generated commands for modules where without most of my flags and didn't link.
If anyone has experience with dividing CMake project into modules, libs and subdirectories, I would gladly accept any help.
I get a message 'can not be used when making a shared object; recompile with -fPIC'
I have try other examples and the issue is the same.
I have try
changing from MODULE to SHARED
cmake .. -DCMAKE_CXX_FLAGS=-fPIC
and other variations
this works:
c++ -c -fPIC -I/usr/include/python3.6m ../account.cpp
c++ -shared -Wall -Werror -Wl,--export-dynamic account.o -L/usr/local/lib -lboost_python36 -o account.so
Here is the basic cmake
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(Test)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
find_package(Boost 1.70.0 COMPONENTS python REQUIRED)
add_library(account SHARED account.cpp)
target_link_libraries(account Boost::python)
target_include_directories(account PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(account PROPERTIES PREFIX "")
Using: make VERBOSE=1 the output commands are:
c++ -DBOOST_ALL_NO_LIB -Daccount_EXPORTS -I/usr/include/python3.6m -isystem /usr/local/include -fPIC -o CMakeFiles/account.dir/account.cpp.o -c /src/boost_python_example/account.cpp
c++ -fPIC -shared -Wl,-soname,account.so -o account.so CMakeFiles/account.dir/account.cpp.o /usr/local/lib/libboost_python36.a
So the cmake is not getting the same paths and flags, I am learning cmake so Im trying to understand this problem. Clearly the problem is not on actual libs but telling cmake where to find the proper ones.
The solution was quite simple. Comparing both commands what was missing on the cmake command was: --export-dynamic
So I solved using option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON) interesting enough the comment is needed.
Working solution:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(Test)
option(BUILD_SHARED_LIBS "Build libraries as shared as opposed to static" ON)
find_package(PythonInterp REQUIRED)
find_package(PythonLibs ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR} EXACT REQUIRED)
find_package(Boost 1.70.0 COMPONENTS python REQUIRED)
add_library(account SHARED account.cpp)
target_link_libraries(account Boost::python)
target_include_directories(account PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(account PROPERTIES PREFIX "")
Thanks everyone for the comments they lead me to a solution
This question already has an answer here:
Flag '-l' in CMAKE_CXX_FLAGS doesn't work
(1 answer)
Closed 4 years ago.
I have a strange behavior where CMake is not able to link any executable if dynamic linking is required. g++ is not able to find any of the symbols defined in the libraries.
I found out it might have to do with the order of the arguments CMake passes to g++ when linking.
Here is the verbose output of the build process (which fails linking):
[ 50%] Building CXX object CMakeFiles/chargen.dir/test.cpp.obj
g++ -std=c++11 -o CMakeFiles/test.dir/test.cpp.obj -c /f/test/test.cpp
[100%] Linking CXX executable mytest
/usr/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
g++ -std=c++11 -lSDL2 CMakeFiles/test.dir/test.cpp.obj -o mytest
Indeed if I try to link using that command I get undefined references, however if I compile with the library flags at the end:
g++ -std=c++11 CMakeFiles/test.dir/test.cpp.obj -o mytest -lSDL2
it runs just fine. How can I force CMake to use that order of arguments instead?
The contents of the CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(mytest)
set(COMPILER_PATH "/usr/bin/")
set(CMAKE_MAKE_PROGRAM "make")
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lSDL2")
set(SOURCE_FILES test.cpp)
add_executable(mytest ${SOURCE_FILES})
You have probably added -lSDL2 into CMAKE_CXX_FLAGS. In that case it will appear withn compile/link flags, in front of source file name in actual link command, as you showed. Don't do that.
You should use _target_link_libraries_ cmake command in order to define libraries to link against. In brief, the skeleton of your CMakeLists.txt should look like this:
project (project_name)
add_executable (mytest test.cpp)
target_link_libraries( mytest SDL2)
In this case, cmake will put libraries on correct place. Note that you may use target_link_libraries after adding target mytest
[EDIT]
After seeing your CMakeLists it is obvious that your problem is in wrong CMAKE_EXE_LINKER_FLAGS. Simply delete the line where you are setting it, and add target_link_libraries after add_executable.
Regarding your question about handling circular dependencies among static libs, you handle it the sam way as you would if you didn't use cmake: mention it twice:
target_link_libraries(
mytest
circular_dep1
circular_dep2
circular_dep1
circular_dep2
)
regarding your question about specific linker flags, just place them into target_link_libraries command, it accepts linker flags as well. You can find documentation on following link:
https://cmake.org/cmake/help/latest/command/target_link_libraries.html
I'm getting undefined reference to 'typeinfo for apache::thrift::transport::TTransportException' (and other symbols) when trying to link my executable with my library which uses thrift. I'm using GCC 7.3.0 on Ubuntu 18.04, building with CMake in CLion
I'm stuck after spending a day googling about this problem and after visiting this, that and this links.
The CMake command looks as following
For my shared object:
TARGET_LINK_LIBRARIES(server INTERFACE
etcdclient
TopologyProtocols
event
${THRIFT_LIBRARIES}
${Boost_LIBRARIES}
lzo2
sqlite3
zmq
${SPDK_LIBS}
${DPDK_LIBS}
grpc
grpc++
gtest
gmock
xml2
stdc++fs
bfd
-l:libisal.so.2
sgutils2
pthread
uuid
rt
)
The executable CMake command:
TARGET_LINK_LIBRARIES(kserver
server
${THRIFT_LIBRARIES}
)
Linker command generated by CMake:
cmake_link_script CMakeFiles/kserver.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG CMakeFiles/kserver.dir/main.cpp.o -o kserver -L/server/ext/spdk/build/lib -L/server/ext/spdk/dpdk/build/lib -L/server/ext/isal/lib -Wl,-rpath,/server/ext/spdk/build/lib:/server/ext/spdk/dpdk/build/lib:/server/ext/isal/lib:/server/cmake-build-release/lib/proj:/usr/local/lib:/server/cmake-build-release/ext/etcd:/server/cmake-build-release/protocols ../../lib/proj/libproj.so /usr/local/lib/libthrift.so /usr/local/lib/libthriftnb.so ../../ext/etcd/libetcdclient.so ../../protocols/libTopologyProtocols.so /home/user/vcpkg/installed/x64-linux/lib/libprotobuf.a -levent /usr/local/lib/libthrift.so /usr/local/lib/libthriftnb.so /home/user/vcpkg/installed/x64-linux/lib/libboost_system.a -llzo2 /home/user/vcpkg/installed/x64-linux/lib/libsqlite3.a -lpthread -ldl -lzmq -lspdk -ldpdk -lgrpc -lgrpc++ -lgtest -lgmock -lxml2 -lstdc++fs -lbfd -l:libisal.so.2 -lsgutils2 -lpthread -luuid -lrt
The command looks perfectly fine, it links with thrift, thriftnb and event
As of compiler and general projects settings, here a content of root CMakeList.txt in the source root
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
LIST(APPEND CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/cmake")
SET(CMAKE_CXX_STANDARD 17)
#SET(ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/lib")
#SET(LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/lib")
#SET(RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/build/bin")
ADD_COMPILE_OPTIONS(
-include server.h
# -Wall
# -pedantic
-march=native
)
ADD_COMPILE_DEFINITIONS(
BOOST_COROUTINES_NO_DEPRECATION_WARNING
GTEST_LINKED_AS_SHARED_LIBRARY
)
INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/lib/include/server
${CMAKE_SOURCE_DIR}/lib/include
)
LINK_DIRECTORIES(
${CMAKE_SOURCE_DIR}/ext/spdk/build/lib
${CMAKE_SOURCE_DIR}/ext/spdk/dpdk/build/lib
${CMAKE_SOURCE_DIR}/ext/isal/lib
)
FIND_PACKAGE(Boost REQUIRED COMPONENTS
system)
FIND_PACKAGE(Protobuf REQUIRED)
FIND_PACKAGE(GRPC REQUIRED)
INCLUDE(${CMAKE_CURRENT_LIST_DIR}/cmake/FindGRPC.cmake)
FIND_PACKAGE(LZO REQUIRED)
FIND_PACKAGE(sqlite3 REQUIRED)
FIND_PACKAGE(ZeroMQ CONFIG REQUIRED)
FIND_PACKAGE(GTest REQUIRED)
FIND_PACKAGE(Thrift REQUIRED)
ADD_SUBDIRECTORY(lib/server)
ADD_SUBDIRECTORY(ext/etcd)
ADD_SUBDIRECTORY(protocols)
ADD_SUBDIRECTORY(proc/kserver)
Sample compilation command
/usr/bin/c++ -DBOOST_COROUTINES_NO_DEPRECATION_WARNING -DGTEST_LINKED_AS_SHARED_LIBRARY -Dserver_EXPORTS -I/home/user/sourcelib/include/server -I/home/user/sourcelib/include -I/home/user/sourceext/spdk/include -I/home/user/sourcecmake-build-release/ext/etcd -I/home/user/sourcecmake-build-release/protocols -I/home/user/vcpkg/installed/x64-linux/include -O3 -DNDEBUG -fPIC -include server.h -march=native -std=gnu++1z -o CMakeFiles/server.dir/misc/ServerHost.cpp.o -c /home/user/sourcelib/server/misc/ServerHost.cpp
What I'm doing wrong?
It took two days to figure out that there was some esoteric version installed on my machine and the package manager was not aware of it, after removing it manually and using version provided by vcpkg everything linked as expected.
I am trying to transfer a camera(non-uvc) stream program to ros platform.
I've already got the camera driver running
and Makefile is like this:
g++ main.cpp -o test_gui -g -I /usr/local/include -L /usr/local/lib -D_LIN -D_DEBUG -L../lib/x64 -I../include -lASICamera -lpthread -lusb -DGLIBC_20 -m64 -lrt -I/opt
Now I want to do this in a ros node, so I write a node in which the CMakelists is like this:
include_directories(
${catkin_INCLUDE_DIRS}
)
include_directories(/usr/local/include)
include_directories(/root/catkin_ws/src/asi_converter_real/include/asi_converter_real)
link_directories(/usr/local/lib)
link_libraries(pthread)
link_libraries(usb)
link_libraries(libASICamera.a)
link_libraries(libASICamera.so)
add_executable(asi_converter_real src/asi_converter_real.cpp)
target_link_libraries(asi_converter_real ${catkin_LIBRARIES})
And the Makefile line generated into /catkin_ws/build/***/ is like this:
/usr/bin/c++ CMakeFiles/asi_converter_real.dir/src/asi_converter_real.cpp.o -o /root/catkin_ws/devel/lib/asi_converter_real/asi_converter_real -rdynamic -L/usr/local/lib -lpthread -lusb -Wl,-Bstatic -lASICamera -Wl,-Bdynamic -lASICamera
But it seems that the system cannot find the dynamic libraries of libusb, coz' it reports
undefined reference to `libusb_set_configuration'
undefined reference to `libusb_claim_interface'
/usr/local/lib/libASICamera.a(ASI174MM.o): In function `WorkingFunc(void*)':
undefined reference to `libusb_bulk_transfer'
undefined reference to `libusb_bulk_transfer'
[1]Is there anyone who knows how to solve this problem ?
[2]How to find the .a and .so of libusb in my computer ? (I am sure I have them, coz' I can pkg-config --cflags/--libs them)
[3]How could I explicitly link a dynamic library in ros CMakelists ? or just link_libraries(usb) is enough for both static and dynamic libriries ?
find_package requires that CMake have a corresponding Find<package>.cmake
Cmake have a pkg-config module.
You can use it to write your own FindLibUSB, as explained on the CMake wiki.
Or you can use it directly in your CMakeLists.txt :
find_package(PkgConfig REQUIRED)
pkg_search_module(LIBUSB1 REQUIRED libusb-1.0)
include_directories(SYSTEM ${LIBUSB1_INCLUDE_DIRS})
You can see in the module documentation all the variables that are set : LIBUSB1_LIBRARIES for the libraries, etc ...
Finding libraries in cmake is typically done using find_package.
In your case it might look like this (taken from this question):
find_package(libusb-1.0 REQUIRED)
include_directories (${LIBUSB_1_INCLUDE_DIRS})
...
target_link_libraries(asi_converter_real ${catkin_LIBRARIES} ${LIBUSB_1_LIBRARIES})
You may have to adjust the version number of course (maybe it can simply be removed).