简体   繁体   English

隐藏gcc arguments到CMake文件

[英]Covert gcc arguments to CMake file

So I am trying to learn CMake and I want to convert the following所以我正在尝试学习 CMake 并且我想转换以下内容

gcc "-DARCH=\"`uname -a`\"" cli_arch.c arch.c 

to a CMakeLists.txt file.到 CMakeLists.txt 文件。

This is what I got so far:这是我到目前为止得到的:

Running cmake -DCMAKE_C_COMPILER=/usr/bin/gcc "-DARCH=\"`uname -a`\""..运行cmake -DCMAKE_C_COMPILER=/usr/bin/gcc "-DARCH=\"`uname -a`\""..

// CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(cli_arch)


add_executable(cli_arch cli_arch.c arch.c)

The result I am getting is:我得到的结果是:

-- The C compiler identification is AppleClang 11.0.3.11030032
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    ARCH

I am not understanding how to pass the arguments "-DARCH=\"`uname -a`\"" to CMake.我不明白如何将 arguments "-DARCH=\"`uname -a`\""传递给 CMake。

The trick is execute_process .诀窍是execute_process

execute_process(COMMAND uname -s
    OUTPUT_VARIABLE uname_output
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

add_executable(cli_arch cli_arch.c arch.c)
target_compile_definitions(cli_arch
    PRIVATE "-DARCH=\"${uname_output}\""
)

I had to use uname -s since at least on my system, uname -a has a # which gcc refuses to accept in a macro via the command line.我不得不使用uname -s因为至少在我的系统上, uname -a有一个 # gcc 拒绝通过命令行在宏中接受。

Edit, since you changed your question.编辑,因为你改变了你的问题。 You're passing ARCH to cmake but not doing anything will it.您正在将ARCH传递给 cmake 但不会执行任何操作。 You can pass the argument along using the same target_compile_definitions line I have above.您可以使用与上面相同的target_compile_definitions行传递参数。

$ cmake -DARCH2=foo .
$ make
$ ./a 
Linux
foo

The relevant part of the new CMakeLists.txt looks like this:CMakeLists.txt的相关部分如下所示:

target_compile_definitions(a
    PRIVATE "-DARCH=\"${uname_output}\""
    PRIVATE "-DARCH2=\"${ARCH2}\""
)

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

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