所以我正在尝试学习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。
窍门是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}\""
)