我目前正在开发一个使用 cmake 的项目。该项目将 alsa 作为可选依赖项。因此,CMakeLists.txt
包含项目。
find_package(ALSA)
不过,我希望构建永远不会针对 alsa 进行构建,即使它在我的系统上。 (有点像 configure
中的 --without-ALSA
)
我想在运行 cmake 时在命令行参数中执行此操作,而不是通过修改CMakeLists.txt
这可以用 cmake 实现吗?
编辑:
或者让项目实现这样的东西会更好吗:
if(WITH_ALSA)
find_package(ALSA REQUIRED)
elseif(NOT WITHOUT_ALSA)
find_package(ALSA)
endif()
编辑2:
最后我使用了 with &没有我上面描述的选项(edit1)。为了具有一点可读性,我定义了一个宏,它实际上为我完成了所有事情。
请参阅 FindOptionalPackage.cmake ://freerdp.com/” rel="nofollow">FreeRDP 项目
I am currently working on a project which is using cmake. The project has alsa as an optional dependency. Thus the projects CMakeLists.txt
contains.
find_package(ALSA)
However I want the build to never build against alsa, even if if is on my system. (Kind of like the --without-ALSA
that would have been in a configure
)
I want to do this in a commandline-parameter when running cmake, not by modifying the CMakeLists.txt
Is this possible with cmake?
EDIT:
OR would it be better to have the project implement something like this:
if(WITH_ALSA)
find_package(ALSA REQUIRED)
elseif(NOT WITHOUT_ALSA)
find_package(ALSA)
endif()
EDIT2:
In the end I used the with & without option I described above (edit1). To have a little readability I defined a macro that acually does everything for me.
See the FindOptionalPackage.cmake in the FreeRDP project
发布评论
评论(2)
首先运行CMake并打开生成的CMakeCache.txt文件。你会发现类似:
ALSA_INCLUDE_DIR=/usr/include/alsa
(你基本上只需要知道变量是如何调用的)。然后你应该能够使用参数调用 cmake
这告诉 cmake,alsa 不在你的系统上。如果这不起作用,请尝试查看包含 alsa 的其他变量并将它们设置为一些指示无法找到库的值。如果 FindALSA.cmake 脚本正确实现(即正确缓存),这应该可以工作。
First run CMake and open the generated CMakeCache.txt file. You will find something like:
ALSA_INCLUDE_DIR=/usr/include/alsa
(you basically just need to know how the variable is called). Then you should be able to call cmake with a parameter
This tells cmake, that alsa is not on your system. If this does not work, try to look at other variables containing alsa and set them to some values indicating the library could not been found. If the FindALSA.cmake script is implemented correctly (i.e. does correct caching), this should work.
上面的答案有效,但看起来并不干净。
在工作中,我们这样做(例如):
或者您可以这样做:
当您配置 cmake 构建时,只需执行
或
它干净,并且它可以保证工作,并且很容易从 cmakefile 中理解和读取。
The above answer works, but doesnt really seem clean.
At work we do this (for example):
or you could do:
when you configure a cmake build, simply do
or
Its clean, and it works guaranteed, and its easy to understand and read from the cmakefile.