CMake是否可以使用g ++编译C文件? [英] Can CMake use g++ to compile C files?

查看:2486
本文介绍了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文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆