簡體   English   中英

在C程序中使用C ++方法

[英]use c++ method in c program

我想在我的C代碼(openwrt的easycwmp包)中使用C ++庫gloox。

我使用openwrt工具鏈將gloox作為包進行編譯:

這是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();
}

頭文件gloox.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代碼中包含gloox.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 

任何幫助表示贊賞

extern“ C”是C ++構造,因此您需要保護標頭,以便可以在C和C ++代碼中使用它,如下所示:

#ifdef __cplusplus
    extern "C" 
    {
#endif

GLOOX_API const char* gloox_version();

#ifdef __cplusplus
    }
#endif

另請注意,即使您的所有代碼都是C,也需要使用C ++前端進行鏈接,因此請使用g ++進行鏈接而不是gcc。

您不能在C代碼(包括在從.c文件使用的.h文件中)中使用extern "C" ,而只能在C ++代碼中使用。

您需要將其包圍#ifdef __cplusplus所以當你它只是活躍#include從.cpp文件,而不是一個.c文件吧。

#ifdef __cplusplus
extern "C"
#endif
GLOOX_API const char* gloox_version();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM