MacOS下CLion使用gtkmm4库的编译器错误解决求助
MacOS下CLion使用gtkmm4编译报错:
-mfpmath=sse等选项被误当作宏定义 问题现象
在MacOS的CLion中配置gtkmm4开发环境,执行「重置缓存并重载项目」时编译器报错,提示-mfpmath=sse、-msse、-msse2不是合法的宏名称,触发3个"macro name must be an identifier"错误。已尝试重装Xcode、clang/llvm,更新Homebrew与MacOS,退出conda环境,问题仍未解决。
原CMake配置
cmake_minimum_required(VERSION 3.25) project(gtk) set(CMAKE_CXX_STANDARD 23) find_package(PkgConfig REQUIRED) pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtkmm-4.0) add_executable(gtk src/main.cpp src/gui.cpp src/gui.hpp src/plot/plotter.cpp src/plot/plotter.hpp src/plot/plotter.cpp src/plot/plotter.hpp src/plot/plottercoords.cpp src/plot/plotterevents.cpp src/plot/plotterlabels.cpp src/plot/plotteroptions.cpp src/plot/plotterseries.cpp src/plot/plotterthemes.cpp src/plot/plotterwidgets.cpp) target_link_libraries(gtk PRIVATE ${GTK4_LIBRARIES}) target_compile_definitions(gtk PRIVATE ${GTK4_CFLAGS_OTHER}) target_include_directories(gtk PRIVATE ${GTK4_INCLUDE_DIRS}) target_link_directories(gtk PRIVATE ${GTK4_LIBRARY_DIRS})
错误输出
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=/usr/bin/make -DCMAKE_C_COMPILER=/usr/local/opt/llvm/bin/clang -G "CodeBlocks - Unix Makefiles" -S /Users/zenith412/Desktop/Saturn/trexs/gtk -B /Users/zenith412/Desktop/Saturn/trexs/gtk/cmake-build-debug -- Configuring done -- Generating done -- Build files have been written to: /Users/zenith412/Desktop/Saturn/trexs/gtk/cmake-build-debug Cannot get compiler information: Compiler exited with error code 1: /usr/local/opt/llvm/bin/clang++ -xc++ -D-mfpmath=sse -D-msse -D-msse2 -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -std=gnu++2b -fpch-preprocess -v -dD -E Homebrew clang version 16.0.4 Target: x86_64-apple-darwin22.5.0 Thread model: posix InstalledDir: /usr/local/opt/llvm/bin (in-process) "/usr/local/Cellar/llvm/16.0.4/bin/clang-16" -cc1 -triple x86_64-apple-macosx13.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name compiler-file18281748440535425928 -mrelocation-model pic -pic-level 2 -mframe-pointer=all -ffp-contract=on -fno-rounding-math -funwind-tables=2 -target-sdk-version=13.3 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=lldb -target-linker-version 857.1 -v -fcoverage-compilation-dir=/Users/shivpatel/Desktop/Saturn/trexs/gtk/cmake-build-debug -resource-dir /usr/local/Cellar/llvm/16.0.4/lib/clang/16 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk -D -mfpmath=sse -D -msse -D -msse2 -D ___CIDR_DEFINITIONS_END -internal-isystem /usr/local/opt/llvm/bin/../include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/local/include -internal-isystem /usr/local/Cellar/llvm/16.0.4/lib/clang/16/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include -std=gnu++2b -fdeprecated-macro -fdebug-compilation-dir=/Users/zenith412/Desktop/Saturn/trexs/gtk/cmake-build-debug -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -dD -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o - -x c++ /private/var/folders/dp/14_5b22171nc4l3vwtn3dyv40000gn/T/compiler-file18281748440535425928 clang -cc1 version 16.0.4 based upon LLVM 16.0.4 default target x86_64-apple-darwin22.5.0 #include "..." search starts here: #include <...> search starts here: /usr/local/opt/llvm/bin/../include/c++/v1 /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/local/include /usr/local/Cellar/llvm/16.0.4/lib/clang/16/include /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/usr/include /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks (framework directory) /Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/Library/Frameworks (framework directory) End of search list. In file included from <built-in>:461: <command line>:1:9: error: macro name must be an identifier #define -mfpmath sse ^ <command line>:2:9: error: macro name must be an identifier #define -msse 1 ^ <command line>:3:9: error: macro name must be an identifier #define -msse2 1 ^ 3 errors generated. [Failed to reload]
问题原因
错误根源在于CMake配置中的target_compile_definitions(gtk PRIVATE ${GTK4_CFLAGS_OTHER}):
${GTK4_CFLAGS_OTHER}变量包含的是编译器选项(如-mfpmath=sse、-msse)target_compile_definitions的作用是添加宏定义,CMake会自动给传入的每个参数加上-D前缀,导致这些编译选项被转换成-D-mfpmath=sse- 编译器将
-D-mfpmath=sse解析为尝试定义名为-mfpmath的宏,而宏名称不能以减号开头,因此触发错误
修复方法
方法1:使用PkgConfig的导入目标(推荐)
直接链接PkgConfig::GTK4导入目标,CMake会自动处理所有编译选项、包含目录和链接库,无需手动配置:
cmake_minimum_required(VERSION 3.25) project(gtk) set(CMAKE_CXX_STANDARD 23) find_package(PkgConfig REQUIRED) pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtkmm-4.0) add_executable(gtk src/main.cpp src/gui.cpp src/gui.hpp src/plot/plotter.cpp src/plot/plotter.hpp src/plot/plottercoords.cpp src/plot/plotterevents.cpp src/plot/plotterlabels.cpp src/plot/plotteroptions.cpp src/plot/plotterseries.cpp src/plot/plotterthemes.cpp src/plot/plotterwidgets.cpp) # 直接链接导入目标,自动处理所有依赖配置 target_link_libraries(gtk PRIVATE PkgConfig::GTK4)
方法2:手动替换编译选项设置
如果不想使用导入目标,将target_compile_definitions替换为target_compile_options(用于添加编译器选项):
cmake_minimum_required(VERSION 3.25) project(gtk) set(CMAKE_CXX_STANDARD 23) find_package(PkgConfig REQUIRED) pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtkmm-4.0) add_executable(gtk src/main.cpp src/gui.cpp src/gui.hpp src/plot/plotter.cpp src/plot/plotter.hpp src/plot/plottercoords.cpp src/plot/plotterevents.cpp src/plot/plotterlabels.cpp src/plot/plotteroptions.cpp src/plot/plotterseries.cpp src/plot/plotterthemes.cpp src/plot/plotterwidgets.cpp) target_link_libraries(gtk PRIVATE ${GTK4_LIBRARIES}) # 用target_compile_options替代target_compile_definitions target_compile_options(gtk PRIVATE ${GTK4_CFLAGS_OTHER}) target_include_directories(gtk PRIVATE ${GTK4_INCLUDE_DIRS}) target_link_directories(gtk PRIVATE ${GTK4_LIBRARY_DIRS})
修改完成后,重新执行「重置缓存并重载项目」即可解决报错。
内容的提问来源于stack exchange,提问作者zenith412
相关产品推荐
相关产品推荐

