我想在我的C代码中使用C++库gloox (用于openwrt的easycwmp包)。
我用openwrt工具在gloox as包中编译:
下面是cpp文件gloox.cpp:
#include "gloox.h"
namespace gloox
{
const std::string XMPP_STREAM_VERSION_MAJOR = "1";
const std::string XMPP_STREAM_VERSION_MINOR = "0";
const std::string GLOOX_VERSION = "1.0.11";
const std::string GLOOX_CAPS_NODE = "http://camaya.net/gloox";
}
extern "C" const char* gloox_version()
{
return gloox::GLOOX_VERSION.c_str();
}头文件glox.h:
#ifndef GLOOX_H__
#define GLOOX_H__
#include "macros.h"
extern "C" //--> error: expected identifier or '(' before string constant
{
GLOOX_API const char* gloox_version();
}
#endif // GLOOX_H__当我在easycwmp包的C代码中包含glox.h时,gloox包的编译是正常的,我得到了这个错误:
staging_dir/target-i386_uClibc-0.9.33.2/usr/include/gloox.h:12:8:错误:字符串常量前应为标识符或'(‘!!
我使用以下命令编译libgloox:
make package/libgloox/compile 然后我用cmd编译easycwmp包:
make package/easycwmp/compile 任何帮助我们都将不胜感激
发布于 2014-11-07 18:33:20
外部"C“是一个C++构造,所以你需要保护你的头,这样它就可以在C和C++代码中使用,如下所示:
#ifdef __cplusplus
extern "C"
{
#endif
GLOOX_API const char* gloox_version();
#ifdef __cplusplus
}
#endif另请注意,您需要使用C++前端进行链接,即使您所有的代码都是C语言,因此使用g++而不是gcc进行链接。
发布于 2014-11-07 18:31:51
不能在C代码中使用extern "C" (包括在从.c文件中使用的.h文件中),只能在C++代码中使用。
您需要使用#ifdef __cplusplus将其包围,以便仅当您从.cpp文件(而不是.c文件)对其执行#include操作时,它才处于活动状态。
#ifdef __cplusplus
extern "C"
#endif
GLOOX_API const char* gloox_version();https://stackoverflow.com/questions/26799027
复制相似问题