将gcc参数转换为CMake文件

问题描述 投票:0回答:1

所以我正在尝试学习CMake,我想转换以下内容

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

到CMakeLists.txt文件。

这是我到目前为止得到的:

正在运行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 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

我不理解如何将参数"-DARCH=\"`uname -a`\""传递给CMake。

c++ c gcc makefile cmake
1个回答
0
投票

窍门是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}\""
)

我必须使用uname -s,因为至少在我的系统上,uname -a有一个#,gcc拒绝通过命令行在宏中接受。

编辑,因为您更改了问题。您正在将ARCH传递给cmake,但不执行任何操作。您可以使用与我上面相同的target_compile_definitions行来传递参数。

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

CMakeLists.txt的相关部分看起来像这样:

target_compile_definitions(a
    PRIVATE "-DARCH=\"${uname_output}\""
    PRIVATE "-DARCH2=\"${ARCH2}\""
)
© www.soinside.com 2019 - 2024. All rights reserved.