Can I use CMAKE_SYSTEM_PROCESSOR, defined in a toolchain file, in CMakeLists?

隐身守侯 提交于 2021-02-10 12:12:21

问题


I'd like to add Raspberry Pi as a cross compilation target to a C++ project which uses CMake. Following the accepted answer to this question, I've set up the environment successfully.

The project has many build targets already, all of them defined in the main CMakeLists.txt in a quite ugly way (it's an old project). In this file, there are some compiler flags set, depending on the CMAKE_SYSTEM_PROCESSOR variable, for example:

if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm")
    # do something
endif ()

According to the guide, I should set this in my Raspberry toolchain file. I've added SET(CMAKE_SYSTEM_PROCESSOR arm), but I can't access this from the CMakeLists, because this becomes an empty string there. I don't have this problem with the other variables, like CMAKE_SYSTEM_NAME. Using message(), I can see that it is still available in the toolchain file, but becomes empty after that.

Is there any way to use CMAKE_SYSTEM_PROCESSOR in CMakeLists.txt? Or, is it available only in the toolchain file intentionally?

I'm using CMake 2.8.12.2.


回答1:


It seems this was a caching issue after all - building in a clean directory fixed the problem. Though I still wonder how did it become an empty string...




回答2:


Looks like you have fallen victim to CMake's double expansion. Try using this instead:

if (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
    # do something
endif ()


来源:https://stackoverflow.com/questions/29986677/can-i-use-cmake-system-processor-defined-in-a-toolchain-file-in-cmakelists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!