如何在C ++项目中使用C源文件? [英] How to use C source files in a C++ project?

查看:134
本文介绍了如何在C ++项目中使用C源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++项目中,由于C和C ++之间的标准不同,包含C源文件的.h文件将引起许多错误。

如何在C ++项目(或主项目)中使用C源文件.cpp)?

In a C++ project, including .h files of C source files will cause many errors because of different standards between C and C++.
How to use C source files in a C++ project (or in main.cpp)?

推荐答案

为了获得最大的可靠性:

For the maximum reliability:


  • 使用C编译器编译C源代码。

  • 使用C ++编译器编译C ++源代码

  • 最好编写主代码()在C ++中起作用。

  • 用C ++编译器链接程序。

  • Compile the C source with a C compiler.
  • Compile the C++ source with a C++ compiler
  • Preferably, write the main() function in C++.
  • Link the program with a C++ compiler.

请确保C标头本身知道C ++或C ++代码在 extern C {...} 块内包含C标头。

Make sure that the C headers are either themselves aware of C++ or that the C++ code includes the C headers inside an extern "C" { ... } block.

要么(C头文件 cheader.h ):

#ifndef CHEADER_H_INCLUDED
#define CHEADER_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

...main contents of header...

#ifdef __cplusplus
}
#endif

#endif /* CHEADER_H_INCLUDED */ 

或(C ++源代码) :

or (C++ source code):

extern "C" {
#include "cheader.h"
}

现代C风格非常接近C和C ++语言的通用子集。但是,由于多种原因,任意C代码都不是C ++代码,并且不能保证仅通过C ++源文件调用C源文件(通过更改扩展名,或者仅通过C ++编译器进行编译)就不会成功。 。通常,比较容易将C编译为C,将C ++编译为C ++,然后将生成的目标文件与C ++编译器链接(以确保调用正确的支持库)。

Modern C style is very close to the common subset of the C and C++ languages. However, arbitrary C code is not C++ code for any of a very large number of reasons, and simply calling the C source files C++ source files (by changing the extension, or simply by compiling with the C++ compiler) is not guaranteed to be successful. In general, it is easier to compile C as C and C++ as C++ and then link the resulting object files with the C++ compiler (to ensure the correct support libraries are invoked).

但是,如果MSVC编译器说使用MFC的程序必须仅用C ++编写( MFC需要C ++编译(使用.cpp后缀)是报告的错误),那么您可能别无选择,只能确保您的C代码可编译为C ++代码。这意味着您必须转换 malloc()等的返回值;您必须担心其他地方不使用强制转换将 void * 转换为其他指针类型;您必须担心C中的 sizeof('a')== 4 sizeof('a')== 1 在C ++中;您必须确保在使用每个函数之前先对其进行声明;您必须确保您的C代码不使用任何C ++关键字(尤其是 typename class ;还包括 inline 有时-但完整的列表很大)。

However, if the MSVC compiler is saying that programs using MFC have to be written solely in C++ (MFC requires C++ compilation (use a .cpp suffix) is the reported error), then you may have no choice but to ensure that your C code is compilable as C++ code. That means you'll have to cast the return values from malloc() et al; you have to worry about other places where you do not use a cast to convert a void * into some other pointer type; you have to worry about sizeof('a') == 4 in C and sizeof('a') == 1 in C++; you have to ensure that every function is declared before it is used; you have to ensure your C code does not use any C++ keywords (typename, class in particular; also inline sometimes — but the complete list is quite large).

在某些圈子中,您必须担心使用情况C99中C ++ 2003或C ++ 2011中所没有的功能,例如灵活的数组成员,指定的初始化程序,复合文字,可变长度数组等。但是,如果C代码是用于MSVC的,那么这可能不会成为问题。 MSVC C编译器不支持这些功能(它仅支持C89,不支持C99)。

In some circles, you'd have to worry about the use of features in C99 that are not in C++2003 or C++2011, such as flexible array members, designated initializers, compound literals, variable-length arrays, and so on. However, if the C code is for MSVC, then that probably isn't going to be a problem; those features are not supported by the MSVC C compiler (it only supports C89, not C99).

FWIW:我有一个脚本可以查找C ++关键字。它包含以下注释:

FWIW: I have a script to hunt down C++ keywords. It contains the following comment:

# http://en.cppreference.com/w/cpp/keywords
# plus JL annotations
# and                               C (<iso646.h>)
# and_eq                            C (<iso646.h>)
# alignas (C++11 feature)
# alignof (C++11 feature)
# asm                               C (core)
# auto(1)                           C (core)
# bitand                            C (<iso646.h>)
# bitor                             C (<iso646.h>)
# bool                              C99 (<stdbool.h>)
# break                             C (core)
# case                              C (core)
# catch
# char                              C (core)
# char16_t (C++11 feature)
# char32_t (C++11 feature)
# class
# compl                             C (<iso646.h>)
# const                             C (core)
# constexpr (C++11 feature)
# const_cast
# continue                          C (core)
# decltype (C++11 feature)
# default(1)                        C (core)
# delete(1)
# double                            C (core)
# dynamic_cast
# else                              C (core)
# enum                              C (core)
# explicit
# export
# extern                            C (core)
# false                             C99 (<stdbool.h>)
# float                             C (core)
# for                               C (core)
# friend
# goto                              C (core)
# if                                C (core)
# inline                            C (core)
# int                               C (core)
# long                              C (core)
# mutable
# namespace
# new
# noexcept (C++11 feature)
# not                               C (<iso646.h>)
# not_eq                            C (<iso646.h>)
# nullptr (C++11 feature)
# operator
# or                                C (<iso646.h>)
# or_eq                             C (<iso646.h>)
# private
# protected
# public
# register                          C (core)
# reinterpret_cast
# return                            C (core)
# short                             C (core)
# signed                            C (core)
# sizeof                            C (core)
# static                            C (core)
# static_assert (C++11 feature)
# static_cast
# struct                            C (core)
# switch                            C (core)
# template
# this
# thread_local (C++11 feature)
# throw
# true                              C99 (<stdbool.h>)
# try
# typedef                           C (core)
# typeid
# typename
# union                             C (core)
# unsigned                          C (core)
# using(1)
# virtual
# void                              C (core)
# volatile                          C (core)
# wchar_t                           C (core)
# while                             C (core)
# xor                               C (<iso646.h>)
# xor_eq                            C (<iso646.h>)

后缀(1)是CPP参考:


  • (1) —在C ++ 11中已更改

  • (1) — meaning changed in C++11

这篇关于如何在C ++项目中使用C源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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