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

编程入门 行业动态 更新时间:2024-10-16 02:22:49
本文介绍了CMake是否可以使用g ++编译C文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

我正在将此项目的构建过程切换为使用CMake。我发现最初CMake想要使用gcc编译C文件。这失败,因为像在使用时声明变量的东西。所以我试图使用g ++编译C文件通过使用设置

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"

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

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.

推荐答案

以此目的。如果你真的需要编译你的C文件作为C ++,那么你应该教会cmake你的文件属于C ++语言:

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 )

更多推荐

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

本文发布于:2023-11-13 22:32:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585506.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   文件   CMake

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!