简体   繁体   English

我可以使用extern“ C” {c的头文件}

[英]can I use extern “C” { headerfile of c }

Instead of writing each function in " extern "C" {} ", can I write entire header file inside that block. 不用在“ extern "C" {} ”中编写每个函数,我可以在该块中写入整个头文件。

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

I have tried this but Its not working at all, why it is not working ? 我已经尝试过了,但是它根本不起作用,为什么它不起作用? if we have to use 100 C functions in a c++ project, do we need provide all the functions in a extern block, is there any other simple way ? 如果我们必须在c ++项目中使用100个C函数,是否需要在extern块中提供所有函数,还有其他简单的方法吗?

Ex: 例如:

extern "C"
{
 void fun1();
 void fun2();
 void fun3();
 void fun4();
 void fun5();
 .
 .
 .
 .
 fun100();
}

Is there any other simple way, like extern "C" { myCfunctions.h } ??? 还有没有其他简单的方法,例如extern "C" { myCfunctions.h } ???

#include simply includes the specified header at the location of the #include . #include仅在#include的位置包含指定的标头。 Whether it's valid depends on what "myCfile.h" contains. 它是否有效取决于"myCfile.h"包含的内容。 In particular, including any standard library headers in such a context is not valid, and may well break on commonly used implementations. 特别是,在这样的上下文中包含任何标准库头都是无效的,并且很可能会破坏常用的实现。

The usual way to handle this is to make the header itself safe to use from C++. 通常的处理方法是使标头本身可以安全地从C ++使用。 A C-only header might contain 仅C的标头可能包含

#ifndef H_MYCFILE
#define H_MYCFILE

#include <stddef.h>

void mycfunc1(void);
void mycfunc2(int i);
void mycfunc3(size_t s);

#endif

Adapting this to make it safe to use from C++: 对此进行修改以使其可以从C ++安全使用:

#ifndef H_MYCFILE
#define H_MYCFILE

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

void mycfunc1(void);
void mycfunc2(int i);
void mycfunc3(size_t s);

#ifdef __cplusplus
}
#endif

#endif

With such a header, you wouldn't be able to safely put the entire header in an extern "C" block. 使用这样的标头,您将无法安全地将整个标头放入extern "C"块中。 However, that header itself can make sure not to put #include <stddef.h> in an extern "C" block, but still to put all function declarations in a single extern "C" block, avoiding having to repeat it for each one. 但是,该标头本身可以确保#include <stddef.h>放在一个extern "C"块中,而仍将所有函数声明放在一个单独的extern "C"块中,避免必须为每个块重复它。

You are doing something wrong. 您做错了。

Because 因为

extern "C" { myCfunctions.h }

should work. 应该管用。 See below sample program. 请参见下面的示例程序。


Lets go by example code. 让我们来看示例代码。

ctest1.c ctest1.c

#include<stdio.h>

void ctest1(int *i)
{
   printf("This is from ctest1\n"); // output of this is missing
   *i=15;
   return;
}

ctest2.c ctest2.c

#include<stdio.h>

void ctest2(int *i)
{
   printf("This is from ctest2\n"); // output of this is missing
   *i=100;
   return;
}

ctest.h ctest.h

void ctest1(int *);
void ctest2(int *);

Now lets make c library from that 现在让我们从中制作c库

gcc -Wall -c ctest1.c ctest2.c
ar -cvq libctest.a ctest1.o ctest2.o

Now lets make cpp based file which will use this c apis prog.cpp 现在,让基于cpp的文件将使用此c apis prog.cpp

#include <iostream>
extern "C" {
#include"ctest.h"
}
using namespace std;

int main()
{
  int x;
  ctest1(&x);
  std::cout << "Value is" << x;
  ctest2(&x);
  std::cout << "Value is" << x;

}

Now lets compile this c++ program with C library 现在让我们用C库编译这个c ++程序

g++ prog.cpp libctest.a

Output is : Value is15Value is100 输出为: 值is15值is100

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

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