You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用CMake检查类型是否存在(不考虑其大小)

Checking if a Type Exists in CMake (Before Using check_type_size)

Great question! When you're working through CMake platform checks, check_type_size is super handy for grabbing the size of a type—but it doesn't explicitly tell you if the type even exists in the first place. Here are the two most reliable approaches to verify a type's existence:

1. Use check_type_size's Implicit Feedback

Believe it or not, check_type_size already gives you a hint about whether the type exists. If the type isn't recognized by the compiler, the result variable you pass (like SIZEOF_MY_TYPE) will be set to NOTFOUND. You can check this variable right after calling the macro:

# First, include the required CMake module
include(CheckTypeSize)

# Attempt to get the size of my_type
check_type_size("my_type" SIZEOF_MY_TYPE)

# Check if the type exists based on the result
if(NOT SIZEOF_MY_TYPE)
    message(STATUS "⚠️ my_type does NOT exist on this platform")
else()
    message(STATUS "✅ my_type exists, size: ${SIZEOF_MY_TYPE} bytes")
endif()

Note: This works for most standard and common custom types, but some compilers might have edge cases (like placeholder types for unknown symbols). For absolute certainty, try the second approach.

2. Explicit Check with try_compile

For full control and no ambiguity, use try_compile to build a tiny test program that attempts to use the type. If the program compiles successfully, the type exists—simple as that.

Example for C types:

# Define a minimal test program that uses my_type
set(TEST_TYPE_SOURCE "
int main() {
    // Declare a variable of the target type (triggers compiler error if it doesn't exist)
    my_type test_var;
    (void)test_var; // Silence unused variable warnings
    return 0;
}
")

# Run the compile check
try_compile(
    MY_TYPE_EXISTS
    ${CMAKE_BINARY_DIR}/type_check_temp  # Temporary build directory
    SOURCE_FROM_CONTENT test_type_check.c "${TEST_TYPE_SOURCE}"
)

# Act on the result
if(MY_TYPE_EXISTS)
    message(STATUS "✅ my_type exists")
    # Now it's safe to call check_type_size if you need the size
    check_type_size("my_type" SIZEOF_MY_TYPE)
else()
    message(STATUS "⚠️ my_type does NOT exist")
endif()

For C++ types:

Just adjust the source file extension to .cpp and tweak the test code if your type lives in a namespace or requires specific headers:

set(TEST_TYPE_SOURCE "
#include <some_cpp_header.h>

int main() {
    my_namespace::my_type test_var;
    (void)test_var;
    return 0;
}
")

try_compile(
    MY_TYPE_EXISTS
    ${CMAKE_BINARY_DIR}/cpp_type_check_temp
    SOURCE_FROM_CONTENT test_type_check.cpp "${TEST_TYPE_SOURCE}"
)

Handling Header Dependencies

If your type is defined in a specific header, don't forget to include that header in your test code! For example:

set(TEST_TYPE_SOURCE "
#include <my_custom_header.h>  // Required for my_type
int main() {
    my_type test_var;
    (void)test_var;
    return 0;
}
")

内容的提问来源于stack exchange,提问作者einpoklum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:45:00