如何部分禁用 cmake C/C++ 自定义编译器检查

2023-08-27C/C++开发问题
91

本文介绍了如何部分禁用 cmake C/C++ 自定义编译器检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 cmake 进行一些交叉编译.互联网上的所有示例都很容易,我设法在 Linux(x86 和 ARM)、Windows 和 Android 上交叉编译了我的库.但现在我想在自定义平台上进行.

I am trying to do some crosscompilation using cmake. Some are easy with all the examples on Internet, I managed to crosscompile my library on Linux (x86 and ARM), Windows and Android. But now I would like to do it on a custom platform.

我需要实现的过程:

  1. 采购我的环境(这会破坏所有以前的 bash 经典环境)
  2. 用cmake编译
  3. 执行我想要的

但是 Cmake 正在测试我的自定义 C/C++ 库中的符号,这使我的库无法编译.我遇到的错误是 cmake 某些版本的 GLIBCXX 和 CXXABI(没有 C 问题)但不是全部.

But Cmake is testing for symbols in my custom C/C++ libraries which make my library unable to compile. The errors I have are that cmake some versions of GLIBCXX and CXXABI (no C issues) but not all of them.

有没有办法让 cmake 可以使用它?

Is there a way to make cmake ok with it ?

我尝试使用:

set(CMAKE_C_COMPILER_WORKS TRUE)
set(CMAKE_CXX_COMPILER_WORKS TRUE)

还有:

include(CMakeForceCompiler)
...
cmake_force_c_compiler(${ENV_PATH}/bin/${CC})
cmake_force_cxx_compiler(${ENV_PATH}/bin/${CXX})

但 cmake 仍在检查符号.

But cmake is still checking for symbols.

推荐答案

如果没有您的环境和错误消息,就不容易说出真正的根本原因,但这里有两个常见原因和相应的修复:

Without having your environment nor the error message it's not easy to tell the actual root cause but here are two of the common causes and respective fixes:

  1. 如果您没有完整的为您的自定义环境创建的工具链文件 - 因此 CMake 无法链接简单的测试程序 - 您可以尝试名为 CMAKE_TRY_COMPILE_TARGET_TYPE.

  1. If you don't have a complete toolchain file created for your custom environment - so CMake can't link a simple test program - you can try the relatively new (version 3.6) global CMake variable named CMAKE_TRY_COMPILE_TARGET_TYPE.

所以只需添加以下内容:

So just add the following:

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

然后 CMake 会尝试构建一个静态库.

Then CMake would just try to build a static library.

要仅设置最常见的 GCC 编译器变量并仅进行一些基本检查,请尝试:

To have only the most common GCC compiler variables set and have only some basic checks, try:

SET(CMAKE_SYSTEM_NAME Generic)

参见 CMake 交叉编译:设置系统和工具链:

如果您的目标是没有操作系统的嵌入式系统,请将 CMAKE_SYSTEM_NAME 设置为通用"

If your target is an embedded system without OS set CMAKE_SYSTEM_NAME to "Generic"

参考资料

  • CMake AMRCC + 自定义链接器
  • cmake 使用特定链接器进行交叉编译不向 armlink 传递参数

这篇关于如何部分禁用 cmake C/C++ 自定义编译器检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6