简体   繁体   English

使用C ++编译器编译C99文件

[英]Compiling C99 files with C++ compiler

Have not found the exact question i am asking in either google or here, everything talks about wanting to call c++ from c code or some part being compiled with c compiler and some other with c++ and then later linked together and the problems that arise from that which i do not want. 还没有找到我要求谷歌或这里的确切问题,一切都谈到想要从c代码调用c ++或者用c编译器和其他一些用c ++编译的一些部分然后将它们链接在一起以及由此产生的问题这是我想要的。

I want to compile and link C99 files with C++ compiler of Visual Studio in my all C++ application and be able to call the c functions without errors and problems.There will be no c linker involved or compiling some part with different compilers and linking together later, or any kind of trick. 我想在我的所有C ++应用程序中使用Visual Studio的C ++编译器编译和链接C99文件,并且能够在没有错误和问题的情况下调用c函数。将不会涉及c链接器或使用不同编译器编译某些部分并在以后链接在一起或任何一种技巧。 The headers are from C library (libcurl) and some others as i want to use them in my application. 标题来自C库(libcurl)和其他一些,因为我想在我的应用程序中使用它们。 I do not want to use the C++ bindings i want to compile c code as c++. 我不想使用C ++绑定,我想将c代码编译为c ++。 Can i trust c code be compiled as C++ code without major refactoring? 我可以相信c代码可以编译为C ++代码而无需重大修改吗? What to do differently than when including C++ headers? 与包含C ++标题时的做法有何不同? What incompatibilities to expect? 有什么不兼容性?

In theory, C code should be able to be compiled as C++ code. 理论上,C代码应该能够编译为C ++代码。 At some point Dr.Stroustrup made the point that all code from ANSI C edition of the K&R compiles with a C++ compiler and has the same semantics as the code compiled with a C compiler has (this was construed that all ANSI C code would be valid C++ code which is, obviously, not the case, eg, because many C++ keywords are not reserved identifiers in C). 在某些时候,Dr.Stroustrup指出,来自K&R的ANSI C版本的所有代码都使用C ++编译器编译,并且具有与使用C编译器编译的代码相同的语义(这被解释为所有 ANSI C代码都是有效的) C ++代码显然不是这种情况,例如,因为许多C ++关键字在C)中不是保留标识符。

However, certain idioms in C will require substantial changes to the C code if you want to compile the code with a C++ compiler. 但是,如果要使用C ++编译器编译代码,C中的某些习语将需要对C代码进行实质性更改。 A typical example is the need to cast void* to the proper type in C++ which isn't needed in C and it seems it is frowned upon casting the result from malloc() to the proper pointer type although the effect is that it prevents the C code from being compiled with a C++ compiler (in my opinion a good think, eg, because there the tighter rules may result in discovering problems in the C code even if the production version is being compiled with a C compiler). 一个典型的例子是需要将void*转换为C ++中的正确​​类型,这在C中是不需要的,并且似乎在将malloc()的结果转换为正确的指针类型时不赞成,尽管效果是它阻止了使用C ++编译器编译的C代码(在我看来很好的思考,例如,因为更严格的规则可能导致在C代码中发现问题,即使生产版本是用C编译器编译的)。 There are also a few subtle semantic differences as far as I know, although right now I can't easily pin-point one of them. 据我所知,还有一些微妙的语义差异,虽然现在我不能轻易地指出其中一个。 That is, the same code compiled with a C and a C++ compiler may have defined but different results for both cases. 也就是说,使用C和C ++编译器编译的相同代码可能已定义但两种情况的结果不同。

In practice, I doubt that you can simply compile a non-trivial body of C code with a C++ compiler and get a program which behaves the same as the original C code. 在实践中,我怀疑你可以简单地用C ++编译器编译一个非平凡的C代码体,并获得一个与原始C代码相同的程序。 If the C program you envision to compile with a C++ comes with a thorough set of test cases it may be feasible to port the code to C++ but it will involve more work than merely renaming the file from <name>.c to <name>.cpp . 如果你想象一个C ++编译C程序自带了彻底的一套测试情况下,可能将代码移植到C可行++,但它会涉及更多的工作不仅仅是重命名从文件<name>.c<name>.cpp I could imagine that a tool could do the required conversions (a compiler compiling C source to C++ source) but I'm not aware of a such a tool. 我可以想象一个工具可以完成所需的转换(编译器将C源编译为C ++源代码),但我不知道这样的工具。 I'm only aware of the opposite direction yielding entirely unreadable code (for example Comeau C++ uses C as a form of portable assembler). 我只知道相反的方向产生完全不可读的代码(例如Comeau C ++使用C作为便携式汇编程序的一种形式)。

If you want to do this using visual studio, then it is not possible. 如果你想使用visual studio这样做,那就不可能了。 MSVC doesn't support C99. MSVC不支持C99。

C and C++ are two different, but closely related, languages. C和C ++是两种不同但密切相关的语言。 C++ is nearly a superset of C++, but not quite (in particular, C++ has keywords that C lacks). C ++ 几乎是C ++的超集,但并不完全(特别是C ++有C缺少的关键字)。

If your code depends on C99 features (ie, features that are in C99 but not in C90), then you may be out of luck. 如果您的代码依赖于C99功能(即C99中的功能但不是C90中的功能),那么您可能会失败。 Microsoft's C compiler does not support C99 (except for a few minor features; I think it permits // comments), and Microsoft has stated clearly that such support is not a priority. 微软的C编译器不支持C99(除了一些小功能;我认为它允许//评论),微软已经明确表示这种支持不是优先考虑的问题。 You may be able to modify the code so it's valid C90, depending on what features it uses. 您可以修改代码,使其有效C90,具体取决于它使用的功能。

Microsoft Visual Studio supports compiling both C and C++ (though it tends to emphasize C++). Microsoft Visual Studio支持编译C和C ++(尽管它倾向于强调C ++)。 If you can get your C code compiling with the MS C compiler, I suggest doing just that rather than treating it as C++. 如果您可以使用MS C编译器编译C代码,我建议这样做,而不是将其视为C ++。 C++ has features, particularly extern "C" , that are specifically designed to let you interface C and C++ code. C ++具有特性,特别是extern "C" ,专门用于连接C和C ++代码。 The C++ FAQ Lite discusses this in section 32 . C ++ FAQ Lite第32节中对此进行了讨论。

If you really need to compile your C code as C++ for some reason, you can probably do so with a few minor source changes. 如果由于某种原因确实需要将C代码编译为C ++,则可以通过一些次要的源更改来完成。 Rename the source file from foo.c to foo.cpp , compile it, and fix any errors that are reported. 将源文件从foo.c重命名为foo.cpp ,编译它,并修复报告的任何错误。 The result probably won't be good C++, but you should be able to get it to work. 结果可能不是很好的 C ++,但你应该能够让它工作。 There are a few constructs that are valid C and valid C++ with different semantics, but there aren't many of them, and you're not likely to run into them (but you should definitely keep that in mind). 有一些构造是有效的C和有效的C ++,具有不同的语义,但它们并不多,而且你不太可能碰到它们(但你应该记住这一点)。

If you want to continue maintaining the code as C++, my advice is to go ahead and make the changes needed to do that, and then stop thinking of it as C code . 如果你想继续将代码保存为C ++,我的建议是继续进行所需的更改,然后停止将其视为C代码

The actual need to compile the same code both as C and as C++ is quite rare. 实际需要以C和C ++编译相同的代码是非常罕见的。 (PJ Plauger, for example, needs to do this, since he provides some libraries intended to be used in either language.) In most cases, C++'s extern "C" and other features are good enough to let you mix the two languages reasonably cleanly. (例如,PJ Plauger需要这样做,因为他提供了一些旨在用于任何一种语言的库。)在大多数情况下,C ++的extern "C"和其他功能足以让你合理地干净地混合这两种语言。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM