强制GCC编译器的.cpp文件保存为C [英] Forcing gcc to compile .cpp file as C
问题描述
我有一个外部提供的.cpp文件。这是和C兼容code的混合物一点C ++为好。 C ++的code只是以C包装采取的C ++功能。
I have an externally provided .cpp file. It is a mixture of C compatible code and a bit of C++ as well. The C++ code is just a wrapper around the C to take advantage of C++ features.
它使用的#ifdef __cplusplus
宏以保护C ++ code,这是伟大的。不幸的是,如果我尝试使用gcc来编译,它会将其作为C ++,因为结尾的文件中。我所知道的gcc和g ++之间的差异 - 我不希望编译为C ++
It uses #ifdef __cplusplus
macros to protect the C++ code, which is great. Unfortunately, if I try to compile using gcc, it treats it as C++ because of the file ending. I'm aware of the differences between gcc and g++ - I don't want to compile as C++.
有没有什么办法可以强制GCC看待这个文件作为C文件?我已经使用例如尝试 - 性病= C99
,但这种正确产生错误C99是无效的C ++
Is there any way I can force gcc to treat this file as a C file? I've tried using e.g. --std=c99
, but this correctly produces the error that C99 isn't valid for C++.
重命名为.c作品的文件,但我想如果可能的话,以避免这一点,因为它的外部提供,它会是很好的为它继续作为一个副本。
Renaming the file to .c works, but I'd like to avoid this if possible because it's externally provided and it'd be nice for it to remain as a pristine copy.
谢谢!
推荐答案
的 -x
选项,GCC,您可以指定所有输入文件的语言,它后面:
The -x
option to gcc lets you specify the language of all input files following it:
$ gcc -x c your-file-name.cpp
如果您只想特殊情况下一个文件,你可以使用 -x无
来关闭特殊处理:
If you only want to special-case that one file, you can use -x none
to shut off the special treatment:
$ gcc -x c your-filename.cpp -x none other-file-name.cpp
(您-filename.cpp
将被编译为C,而等文件,name.cpp
将使用扩展和编译为C ++)
(your-filename.cpp
will be compiled as C, while other-file-name.cpp
will use the extension and compile as C++)
这篇关于强制GCC编译器的.cpp文件保存为C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!