skip to Main Content

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


  1. Chosen as BEST ANSWER

    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)
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search