Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++;项目 在C++项目中,包括C源文件的H文件,由于C和C++之间的标准不同,会造成许多错误。 如何在C++项目(或在Me.CPP)中使用C++源文件?< P> C++,将“向后兼容性”称为C源代码,这样的选择是将C源复制到.CPP文件并生成。 现在C++是“强”>不是完全> 向后兼容,因此可能需要更改C源中的一些东西,但通常它应该以最小的错误构建。只需确保包含.C使用的C库(考虑到编译器也支持C) #包括 #包括 //诸如此类_C++_C - Fatal编程技术网

如何在C++;项目 在C++项目中,包括C源文件的H文件,由于C和C++之间的标准不同,会造成许多错误。 如何在C++项目(或在Me.CPP)中使用C++源文件?< P> C++,将“向后兼容性”称为C源代码,这样的选择是将C源复制到.CPP文件并生成。 现在C++是“强”>不是完全> 向后兼容,因此可能需要更改C源中的一些东西,但通常它应该以最小的错误构建。只需确保包含.C使用的C库(考虑到编译器也支持C) #包括 #包括 //诸如此类

如何在C++;项目 在C++项目中,包括C源文件的H文件,由于C和C++之间的标准不同,会造成许多错误。 如何在C++项目(或在Me.CPP)中使用C++源文件?< P> C++,将“向后兼容性”称为C源代码,这样的选择是将C源复制到.CPP文件并生成。 现在C++是“强”>不是完全> 向后兼容,因此可能需要更改C源中的一些东西,但通常它应该以最小的错误构建。只需确保包含.C使用的C库(考虑到编译器也支持C) #包括 #包括 //诸如此类,c++,c,C++,C,如果您只是使用源代码而不是一些预编译库,在大多数情况下,您可以将.c文件重命名为.cpp文件,以获得最大的可靠性: 使用C编译器编译C源代码 用C++编译器编译C++源代码 最好在C++中编写主函数(./LI>) 将程序与C++编译器链接。 确保C标头本身或C++,或者C++代码包含在外部“C”{…} /COD>块中的C标题。 (C头文件cheader.h): 或(C++源代码): 现代C风格非常接近C语言和C++语言的公共子集。然而,任意C代码不是C++代码中任意一个原因非常多的,而简单调

如果您只是使用源代码而不是一些预编译库,在大多数情况下,您可以将.c文件重命名为.cpp文件,以获得最大的可靠性:


  • 使用C编译器编译C源代码
  • 用C++编译器
  • 编译C++源代码
  • 最好在C++中编写主函数(./LI>) 将程序与C++编译器链接。

确保C标头本身或C++,或者C++代码包含在<代码>外部“C”{…} /COD>块中的C标题。 (C头文件

cheader.h
):

或(C++源代码):

现代C风格非常接近C语言和C++语言的公共子集。然而,任意C代码不是C++代码中任意一个原因非常多的,而简单调用C源文件C++源文件(通过改变扩展,或者简单地通过编译C++编译器)是不保证成功的。一般来说,C和C++作为C++编译更容易,然后将生成的对象文件与C++编译器链接(以确保调用正确的支持库)。 <>但是,如果MSVC编译器说使用MFC的程序必须完全在C++中编写(MFC需要C++编译(使用.CPP后缀)是报告错误),那么您可能别无选择,只能确保C代码可编译为C++代码。这意味着您必须从

malloc()
等转换返回值;您必须担心其他不使用强制转换将
void*
转换为其他指针类型的地方;C++中的代码> > siZeOf(‘a’)=4</Cube>C和<代码> siZeof(‘a’)=1 < /COD>您必须确保每个函数在使用前都已声明;你必须确保你的C代码不使用任何C++关键字(<代码>类型名< /COD>,<代码>类< /代码>;有时也>代码>内联有时-但完整的列表相当大。

在某些情况下,您必须担心C99中不在C++2003或C++2011中的功能的使用,例如灵活的数组成员、指定的初始值设定项、复合文本、可变长度数组等等。然而,如果C代码是针对MSVC的,那么这可能不会是一个问题;MSVC C编译器不支持这些功能(它只支持C89,不支持C99)

我有一个脚本来搜索C++关键词。它包含以下评论:

# 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>)
#http://en.cppreference.com/w/cpp/keywords
#加上JL注释
#和C()
#和_eqc()
#alignas(C++11功能)
#对齐(C++11功能)
#高级文书主任(核心)
#自动(1)C(核心)
#bitc()
#比特数C()
#布尔C99()
#断路器C(核心)
#案例C(核心)
#抓住
#字符C(核心)
#char16_t(C++11特性)
#char32_t(C++11特性)
#阶级
#compl C()
#常数C(核心)
#constexpr(C++11特性)
#康斯塔
#继续C(核心)
#decltype(C++11功能)
#默认值(1)C(核心)
#删除(1)
#双C(核心)
#动态浇铸
#其他C(核心)
#枚举C(核心)
#明确的
#出口
#外部C(核心)
#假C99()
#浮点数C(核心)
#对于C(核心)
#朋友
#转到C(核心)
#如果是C(核心)
#内联C(核心)
#内部C(核心)
#长C(核心)
#易变的
#名称空间
#新的
#无例外(C++11特性)
#不是C()
#非_eqc()
#nullptr(C++11特性)
#接线员
#或C()
#或_eqc()
#私人的
#保护
#公开的
#寄存器C(核心)
#重新解释
#返回C(核心)
#短C(核心)
#签名C(核心)
#C(芯材)尺寸
#静态C(核心)
#静态断言(C++11特性)
#静态浇铸
#结构C(核心)
#交换机C(核心)
#模板
#这个
#线程本地(C++11功能)
#扔
#真C99()
#试一试
#类型定义C(核心)
#类型ID
#字体名
#工会C(核心)
#无符号C(核心)
#使用(1)
#虚拟的
#空隙C(核心)
#挥发性C(核心)
#wchar_t C(核心)
#而C(核心)
#异或C()
#xor_等式C()
(1)
后缀是CPP参考中的脚注:

<
#ifndef CHEADER_H_INCLUDED
#define CHEADER_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

...main contents of header...

#ifdef __cplusplus
}
#endif

#endif /* CHEADER_H_INCLUDED */ 
extern "C" {
#include "cheader.h"
}
# 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>)
#include <cassert>

#include "c.h"

int main() {
    assert(f() == 1);
}
#ifndef C_H
#define C_H

/* This ifdef allows the header to be used from both C and C++. */
#ifdef __cplusplus
extern "C" {
#endif
int f();
#ifdef __cplusplus
}
#endif

#endif
#include "c.h"

int f() { return 1; }
g++ -c -o main.o -std=c++98 main.cpp
gcc -c -o c.o -std=c89 c.c
g++ -o main.out main.o c.o
./main.out
#include <assert.h>

#include "cpp.h"

int main(void) {
    assert(f_int(1) == 2);
    assert(f_float(1.0) == 3);
    return 0;
}
#ifndef CPP_H
#define CPP_H

#ifdef __cplusplus
// C cannot see these overloaded prototypes, or else it would get confused.
int f(int i);
int f(float i);
extern "C" {
#endif
int f_int(int i);
int f_float(float i);
#ifdef __cplusplus
}
#endif

#endif
#include "cpp.h"

int f(int i) {
    return i + 1;
}

int f(float i) {
    return i + 2;
}

int f_int(int i) {
    return f(i);
}

int f_float(float i) {
    return f(i);
}
gcc -c -o main.o -std=c89 -Wextra main.c
g++ -c -o cpp.o -std=c++98 cpp.cpp
g++ -o main.out main.o cpp.o
./main.out