2

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

klementuel
  • 43
  • 4
  • 1
    Did you consider just using [GNU make](https://www.gnu.org/software/make/)? You could install [Debian](http://debian.org/) on your laptop and use [GCC](http://gcc.gnu.org/) as a *cross* compiler! See [this draft report](http://starynkevitch.net/Basile/bismon-doc.pdf) for details. You don't need to run GCC on your Raspberry Pi! – Basile Starynkevitch Mar 08 '21 at 17:58
  • 1
    Building with CMake won't be faster than a plain compiler invocation. "Especially the -flto=1 which limits the parallel flags to 1" - Since this is a compiler flag, you can google for it. See e.g. [that question](https://stackoverflow.com/questions/11783932/how-do-i-add-a-linker-or-compile-flag-in-a-cmake-file). "I also don't quite understand how to actually integrate the libraries" - again, just google for that. And never assign to variable `CMAKE_BINARY_DIR`, this variable has special meaning for CMake and should be treated as readonly. – Tsyvarev Mar 08 '21 at 18:34
  • @BasileStarynkevitch yes, I thought of that and am working on it in parallel, but it also takes a long time to build on my debian vm, about 15 minutes. However, I have not set the -flto=1 flag here. – klementuel Mar 08 '21 at 19:03
  • @Tsyvarev ok good to know, got the tip that it is faster with the make -j command when building, especially when i only change one line in my source code... – klementuel Mar 08 '21 at 19:06

2 Answers2

1

You can use SET() to set the compiler flags. Check the CMake documentation

You can set the CMAKE_CXX_FLAGS to the flags you want to pass to the compiler

jordanvrtanoski
  • 5,104
  • 1
  • 20
  • 29
1

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.

cmake_minimum_required(VERSION 3.10)

# set the project name
project(MotorSteuerung)

message("Cmake Prefix Path is \"${CMAKE_PREFIX_PATH}\"")
# open62541 must be installed.
# If in custom path, then use -DCMAKE_PREFIX_PATH=/home/user/install
find_package(open62541 1.1 REQUIRED COMPONENTS FullNamespace)

#The following folder will be included
include_directories(${CMAKE_PREFIX_PATH}/include)
link_directories(${CMAKE_PREFIX_PATH}/lib)

# add the executable
add_executable(MotorSteuerung main.c MotorSteuerung.c)

#The following libraries will be linked
target_link_libraries(MotorSteuerung open62541 mbedtls mbedx509 mbedcrypto)

#Limit Parallel Linking Jobs with -DMY_LIMIT_FLAG=ON
option(MY_LIMIT_FLAG "If Build fail you can set this ON to Limit parallel Linking Jobs to One" OFF) #OFF by default
if(MY_LIMIT_FLAG)
    set(CMAKE_C_FLAGS "-flto=1")
    message("Set Parallel Linking Jobs to One")
endif(MY_LIMIT_FLAG)
unset(MY_LIMIT_FLAG CACHE) # <---- this is the important!!

#Set the the C Standart
set_property(TARGET MotorSteuerung PROPERTY C_STANDARD 99)
klementuel
  • 43
  • 4