CMake 可以使用 g++编译C文件?

发布于 2024-12-09 03:08:42 字数 446 浏览 0 评论 0原文

我曾参与过一个项目,其中使用 g++ 编译以 .c 结尾的文件中的 C 代码。原因是我被告知 g++ 有更好的警告消息。

我正在将该项目的构建过程切换为使用 CMake。我发现最初CMake想使用gcc来编译C文件。由于在使用时声明变量之类的原因,此操作失败了。 设置来使用g++编译C文件

set(CMAKE_C_COMPILER_INIT g++)

所以我尝试通过使用CMakeLists.txt文件中的 。但这会导致错误消息:

#error "The CMAKE_C_COMPILER is set to a C++ compiler"

我已将 .c 文件重命名为 .cpp 以解决此问题,因为这似乎是我使事情正常工作的最简单方法,也许也是最好的方法。但我想知道是否可以强制 CMake 使用 g++ 来编译 C 文件。

I have worked on a project where I was using g++ to compile C code in files that end in .c. The reason is that I'm told that g++ has better warning messages.

I am switching the build process for this project to use CMake. I found that initially CMake wanted to use gcc to compile C files. This failed because of things like declaring variables at use time. So I tried to use g++ to compile C files by using the setting

set(CMAKE_C_COMPILER_INIT g++)

in the CMakeLists.txt file. But this results in the error message:

#error "The CMAKE_C_COMPILER is set to a C++ compiler"

I have been renaming my .c files to .cpp to fix this problem as that seems to be the easiest way for me to make things work, and perhaps the best way too. But I was wondering if it is possible to force CMake to use g++ to compile C files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半衾梦 2024-12-16 03:08:42

您不应该为此目的重写编译器。如果您确实需要将 C 文件编译为 C++,那么您应该告诉 cmake 您的文件属于 C++ 语言:

set_source_files_properties(filename.c PROPERTIES LANGUAGE CXX )

You should not override the compiler for this purpose. If you really need to compile your C files as C++ then you should teach cmake that your files belong to C++ language:

set_source_files_properties(filename.c PROPERTIES LANGUAGE CXX )
十年九夏 2024-12-16 03:08:42

要让 cmake 将所有 C 文件视为 C++ 文件,请使用:

file(GLOB_RECURSE CFILES "${CMAKE_SOURCE_DIR}/*.c")
SET_SOURCE_FILES_PROPERTIES(${CFILES} PROPERTIES LANGUAGE CXX )

To have cmake treat all C files as C++ files use:

file(GLOB_RECURSE CFILES "${CMAKE_SOURCE_DIR}/*.c")
SET_SOURCE_FILES_PROPERTIES(${CFILES} PROPERTIES LANGUAGE CXX )
捂风挽笑 2024-12-16 03:08:42

如果需要切换整个项目,请在项目 指令:

project(derproject LANGUAGES CXX)

If you need to switch the whole project, set it in the project directive:

project(derproject LANGUAGES CXX)
惟欲睡 2024-12-16 03:08:42

set_source_files_properties

这里(我)选择的 CMake 设置是 set_source_files_properties 命令。 https://cmake.org/cmake/help/latest/command/set_source_files_properties。正如

set(qpid_dispatch_SOURCES
  alloc.c
  alloc_pool.c
  aprintf.c
  amqp.c
  atomic.c
# [...]
)
set_source_files_properties(${qpid_dispatch_SOURCES} PROPERTIES LANGUAGE CXX)
add_library(qpid-dispatch OBJECT ${qpid_dispatch_SOURCES})

链接文档中所述,CMake 3.18 更改了 set_source_files_properties 的范围效果。请参阅 DIRECTORYTARGET_DIRECTORY 选项。因此,要将源文件属性递归地应用于项目中的所有文件,您的 CMakeLists.txt 应如下所示

cmake_minimum_required(VERSION 3.20)
project(qpid-dispatch LANGUAGES C CXX)

# [...]

add_subdirectory(src)
add_subdirectory(tests)
add_subdirectory(router)

# [...]

file(GLOB_RECURSE CFILES "*.c")
set_source_files_properties(${CFILES}
        DIRECTORY src tests router
        PROPERTIES LANGUAGE CXX)

set_source_files_properties

The CMake setting of (my) choice here would be the set_source_files_properties command. https://cmake.org/cmake/help/latest/command/set_source_files_properties.html

set(qpid_dispatch_SOURCES
  alloc.c
  alloc_pool.c
  aprintf.c
  amqp.c
  atomic.c
# [...]
)
set_source_files_properties(${qpid_dispatch_SOURCES} PROPERTIES LANGUAGE CXX)
add_library(qpid-dispatch OBJECT ${qpid_dispatch_SOURCES})

As described in the linked docs, CMake 3.18 changed the scoped effect of set_source_files_properties. See the DIRECTORY and TARGET_DIRECTORY options. Therefore, to apply source file property recursively to all files in your project, your CMakeLists.txt should look something like this

cmake_minimum_required(VERSION 3.20)
project(qpid-dispatch LANGUAGES C CXX)

# [...]

add_subdirectory(src)
add_subdirectory(tests)
add_subdirectory(router)

# [...]

file(GLOB_RECURSE CFILES "*.c")
set_source_files_properties(${CFILES}
        DIRECTORY src tests router
        PROPERTIES LANGUAGE CXX)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文