I hope the question is not too trivial, but I am trying to convert a gcc command into a CMakeLists.txt. Unfortunately I am failing at understanding the basics and I hope you can help me.
My main reason for wanting to do this is that I’m building the whole thing on a Raspberry and it has quite a few performance problems. So it takes a good 90min to build the whole thing. With cmake I hope that the build process is done faster when changes are made. Which should be theoretically possible.
Here is the my gcc command
gcc -std=c99 -flto=1 -I$HOME/install/include -L$HOME/install/lib main.c MotorSteuerung.c
-lopen62541 -lmbedtls -lmbedx509 -lmbedcrypto -o MotorSteuerung
I had already started with the cmake file, but unfortunately I don’t quite understand what I have to do. Especially the -flto=1 which limits the parallel flags to 1 (otherwise it can’t be built on the raspberry) led me here after a long search. But I also don’t quite understand how to actually integrate the libraries…
cmake_minimum_required(VERSION 3.20)
# set the project name
project(MotorSteuerung)
# Set the output folder where your program will be created
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
# The following folder will be included
include_directories("${PROJECT_SOURCE_DIR}")
# add the executable
add_executable(Tutorial open62541::open62541)
Thanks for your help
2
Answers
The tip with Set() and CMAKE_CXX_FLAGS helped me a lot. I have now put the rest together and tried it out. Unfortunately, as mentioned in the comments, this had no effect on the build time, but the handling is now much better than with the gcc command. Here you can find my cmake file, maybe it will help some of you to translate your gcc command.
You can use
SET()
to set the compiler flags. Check the CMake documentationYou can set the
CMAKE_CXX_FLAGS
to the flags you want to pass to the compiler