包装用的extern&QUOT一个C的lib; C"除了内部的C ++包括 [英] Wrapping a C lib with extern "C" except an internal C++ include
问题描述
我有一个C库,我需要在C ++ code使用,所以我需要一个的externC
块来包装整个库。问题是,该库似乎包括一个C ++编译code,所以包裹整个lib中也将包裹该C ++头
I have a C library that I need to use in a C++ code, so I need to wrap the whole lib with an extern "C"
block. The problem is that the library seems to include a C++ compiled code, so wrapping the whole lib would also wrap that C++ header.
在 lib.h
我只包括所有内部接头我要揭露,是这样的:
Inside lib.h
I only include all the internal headers I want to expose, something like this:
#ifndef LIB_H
#define LIB_H
#include "lib_foo.h"
#include "lib_bar.h"
#include "lib_baz.h"
#endif
因此,客户只需要包括 lib.h
用lib。
在我第一次尝试我做这个:
In my first attempt I've done this:
#ifndef LIB_H
#define LIB_H
extern "C" {
#include "lib_foo.h"
#include "lib_bar.h"
#include "lib_baz.h"
}
#endif
但后来我得到一个符号查找错误,当我执行在 already_compiled_c ++任何函数的.h
。
我如何从应用的externC
在 already_compiled_c ++的.h
头文件避免?
How can I avoid from applying extern "C"
in the already_compiled_c++.h
header file?
编辑:
解决。这不是使用的externC
的一个问题,它是贯通编译的C ++库石膏正确一个问题:的在绞股蓝皂苷使用共享库节点sqlite3的
Solved. This was not a problem using extern "C"
, it was a problem linking the compiled c++ library with gyp correctly: Using shared library in Gyp in node-sqlite3
推荐答案
注意外部C
是不合法的C,所以它必须只包含母鸡编译为C ++
Note that extern C
isn't legal C, so it must only be included hen compiling as C++.
该already_compiled_c ++ h头可能包含针对多种包括防护,所以只是包括它第一次:
The already_compiled_c++.h header probably contains a guard against multiple includes, so just include it first:
#ifndef LIB_H
#define LIB_H
# This include added so that it won't get marked extern "C" when included by lob_foo.h.
#include <already_compiled_c++.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "lib_foo.h"
#include "lib_bar.h"
#include "lib_baz.h"
#ifdef __cplusplus
}
#endif
#endif
注意:您需要检查是否 already_compiled_c + + H
有条件列入,并添加相应的条件语句
Note: You need to check if already_compiled_c++.h
is conditionally included and add the corresponding conditionals.
这篇关于包装用的extern&QUOT一个C的lib; C&QUOT;除了内部的C ++包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!