简体   繁体   English

将 Linux Debian gcc 命令转换为 CMakeLists.txt

[英]Convert Linux Debian gcc command to CMakeLists.txt

I hope the question is not too trivial, but I am trying to convert a gcc command into a CMakeLists.txt.我希望这个问题不是太琐碎,但我正在尝试将 gcc 命令转换为 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.所以构建整个东西需要90分钟。 With cmake I hope that the build process is done faster when changes are made.对于 cmake,我希望在进行更改时能够更快地完成构建过程。 Which should be theoretically possible.这在理论上应该是可能的。

Here is the my gcc command这是我的 gcc 命令

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.我已经开始使用 cmake 文件,但不幸的是我不太明白我必须做什么。 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.特别是 -flto=1 将并行标志限制为 1(否则它不能在树莓上构建)在经过长时间的搜索后引导我来到这里。 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谢谢你的帮助

You can use SET() to set the compiler flags.您可以使用SET()设置编译器标志。 Check the CMake documentation查看CMake 文档

You can set the CMAKE_CXX_FLAGS to the flags you want to pass to the compiler您可以将CMAKE_CXX_FLAGS设置为要传递给编译器的标志

The tip with Set() and CMAKE_CXX_FLAGS helped me a lot. Set() 和 CMAKE_CXX_FLAGS 的提示对我帮助很大。 I have now put the rest together and tried it out.我现在已经把 rest 放在一起试用了。 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.不幸的是,正如评论中提到的,这对构建时间没有影响,但现在处理比使用 gcc 命令要好得多。 Here you can find my cmake file, maybe it will help some of you to translate your gcc command.在这里你可以找到我的 cmake 文件,也许它会帮助你们中的一些人翻译你的 gcc 命令。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM