[英]Code Blocks - mixing c and c++ files
So far I've been working with g++ (on Windows), but for some reasons I want to use Code:Blocks to use sfml library (quite easy configuration, contrary to clean g++). 到目前为止,我一直在使用g ++(在Windows上),但是由于某些原因,我想使用Code:Blocks来使用sfml库(相当简单的配置,与干净的g ++相反)。
I use bison to create file.tab.c and file.tab.h (from file.y), I use flex for lex.yy.c (from file.l) and i have my own files containing functions needed for my project (file.cpp and file.h). 我使用野牛来创建file.tab.c和file.tab.h(来自file.y),我将flex用于lex.yy.c(来自file.l),我有自己的文件,其中包含项目所需的功能(file.cpp和file.h)。 So with g++ I compile it like that:
因此,使用g ++可以像这样编译它:
g++ lex.yy.c file.tab.c file.cpp -o file.exe
and it works fine. 而且效果很好。 Now i want to compile these files with Code:Blocks and i get a strange error:
现在我想用Code:Blocks编译这些文件,并且出现一个奇怪的错误:
file.l:4:18: fatal error: cstdio: No such file or directory
Why on earth does it signal an error in file.l when this file isn't even included to my project (it just served to create lex.yy.c which i attached to project)? 当这个文件甚至不包含在我的项目中时(为什么它只是用来创建我附加到项目中的lex.yy.c),为什么它实际上在file.l中指示错误? And I'm not sure about that but... main() is in file.tab.c so this is an entry point - can I specifically ask Code:Blocks to compile everything with g++ like I did in command line and there would be a chance for it to work?
而且我不确定,但是... main()在file.tab.c中,所以这是一个切入点-我可以专门要求Code:Blocks像在命令行中一样用g ++编译所有内容,然后有机会工作吗? I have the latest version of Code:Blocks.
我拥有最新版本的Code:Blocks。 Thanks in advance
提前致谢
//Edit: I can also add that i get the same error when i compile it from command line like that: gcc lex.yy.c. //编辑:我还可以补充一点,当我从命令行编译它时,会得到相同的错误:gcclex.yy.c。
To make all the source files in a C::B project be compiled as C++, regardless of file extension and regardless of whether gcc
or g++
is invoked, add the option -x c++
in YourProject -> Build options... -> Compiler settings -> Other options . 要使C :: B项目中的所有源文件都编译为C ++,而不考虑文件扩展名和调用
gcc
还是g++
,请在YourProject- > Build options ...- > Compiler中添加选项-x c++
。 设置 -> 其他选项 。
That forces C++ compilation and should solve your problem, but just in case it is not clear to you, the error: 这会强制进行C ++编译并应解决您的问题,但是以防万一您不清楚,该错误:
fatal error: cstdio: No such file or directory
is occurring because the C++ header <cstdio>
is being #include
-ed in a compilation that is being compiled as C, and that header is not in the C compiler's include-search paths. 发生这种情况是因为C ++头文件
<cstdio>
被#include
include-包含在正在编译为C的编译中,并且该头文件不在C编译器的include-search路径中。
The C compiler is being chosen based on the file extension ( .c
) of the file being compiled when the error occurs. 发生错误时,将基于正在编译的文件的文件扩展名(
.c
)选择C编译器。 The -x c++
option overrides this, but you might look for a better solution by investigating how it happens that a C++ header is getting included in a C source file, and correcting that if you can. -x c++
选项将覆盖此问题,但是您可能需要调查一下C ++头文件被包含在C源文件中的情况,并可能的话进行更正,以寻求更好的解决方案。
The C header equivalent to <cstdio>
is <stdio.h>
. 等同于
<cstdio>
的C头文件是<stdio.h>
。 Can you fix the problem simply by changing <cstdio>
to <stdio.h>
somewhere? 您能否仅通过将
<cstdio>
更改为<stdio.h>
来解决问题? If you use the -x c++
solution then it stops you from ever adding a source file to the project that should be compiled as C. 如果您使用
-x c++
解决方案,那么它将阻止您将源文件添加到应编译为C的项目中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.