在C项目中使用C ++库导致一长串错误 [英] Using a C++ Library in a C Project causes a long list of errors

查看:269
本文介绍了在C项目中使用C ++库导致一长串错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 C ++库 C项目。但是,如果我编译C项目,我得到一个很长的列表未定义的引用XY错误。



这些错误是由C ++引起的参考:



编译时发生错误:

 在函数`< c function>'中:
隐式声明函数`< function>'
[...]

(列表短路)

其中< c function> 是来自C项目和< function> 来自C ++库的函数。



链接时出错:

 未定义的引用`std :: cerr'
未定义的引用到`std :: basic_ostream< char,std :: char_traits<炭> >&安培;的std ::运营商LT;< <的std :: char_traits<炭> >(std :: basic_ostream< char,std :: char_traits< char>&&& char char * *)'
未定义的引用`std :: basic_ostream< char,std :: char_traits< char> >&安培;的std ::运营商LT;< < char,std :: char_traits< char>,std :: allocator< char> >(std :: basic_ostream< char,std :: char_traits< char>&&&&& std :: basic_string< char,std :: char_traits< char>,std :: allocator< char>> const&
[...]
未定义的引用`__gxx_personality_v0'
更多未定义的引用`__gxx_personality_v0'跟随

(很多错误 - 示例短路)



在静态C ++库中,我的类型/由 externC封闭

  #ifdef __cplusplus 
externC
{
#endif

void example();

#ifdef __cplusplus
}
#endif

为了确保这一点,所有的 #include 引用C ++库都被这样一个 externC



编译器:使用 g ++ 编译 C项目使用 gcc - 均来自 MinGW



CL链接): gcc -L< search-path here> -shared [...] -lstdc ++ -l< C ++库set here>



请注意 -lstdc ++ 标志



注意











编辑:



使用 g ++ 作为链接器:未定义引用 std :: cerr'`等已经没了,但是我收到另一个错误列表:

 警告:.drectve`-aligncomm:___ hexdig_D2A,5'无法识别的
未定义引用`<来自C ++库的函数/类/方法>'
[...]
未定义的引用`_Unwind_Resume'
[...]
未定义引用`__gxx_personality_v0'
更多未定义的引用`__gxx_personality_v0'跟随
未定义引用`__chkstk_ms'
[...]

(list shorted)



_Unwind_Resume 在c ++库的 nm 中可用。






示例:



这部分在库(C ++)中:



Test.h - 标题

  #ifndef TEST_H_ 
#define TEST_H_

// ---纯C ++这里---
#ifdef __cplusplus

#include< string> / *(1)* /
class Test
{
public:
Test();


void aMethod();

private:
int i;
};
#endif

// --- Mixed(C / C ++)here ---

#ifdef __cplusplus
externC
{
#endif

extern void doTest();
extern void invoke();

#ifdef __cplusplus
}
#endif

#endif / * TEST_H_ * /
pre>

Test.cpp - 执行C ++部分

  #includeTest.h


Test :: Test():i(0){}

void Test :: aMethod ()
{
this-> i ++;
}


void invoke()
{
测试我;
i.aMethod();
}

Test.c - C接口的实现仍然在图书馆!)

  #includeTest.h

void doTest()
{
invoke();
}

现在,这里是第二个项目(纯C)的一部分: / p>

  #includeTest.h


int main(int argc,char * argv [])
{
doTest();

return 0;
}

(1) :如果我删除包含我可以编译C项目。但是,添加一个包含任何类或c ++的东西会导致很多错误,而链接。不幸的是,我需要包括课程等。



Btw。这里是这个例子的错误:

  Test.cpp :( .text + 0x1b):未定义的引用`std: :basic_string< char,std :: char_traits< char>,std :: allocator< char> > :: basic_string()'
Test.cpp :(。文本+ 0x64):未定义的引用`_Unwind_Resume'
Test.cpp :(。文本$ _ZN4TestD1Ev [Test ::〜Test() ] + 0x12):未定义的引用到`std :: basic_string< char,std :: char_traits< char>,std :: allocator< char> > ::〜basic_string()'
Test.cpp :(。eh_frame + 0x6b):未定义的引用`__gxx_personality_v0'

设置 -lstdc ++ 标记之后(!)库的标志我得到只这些:

  Test.cpp :(。text + 0x64):未定义的引用`_Unwind_Resume'
Test.cpp :(。eh_frame + 0x6b):未定义的引用到`__gxx_personality_v0'


解决方案

MinGW GCC。




  • 使用 GCC 3.4.5 错误 / li>
  • 使用 GCC 4.7.2 无错误



还要确保 -lstdc ++ -l <​​/ $ c的最后(!) $ c> flags。


I have a C Project which uses a C++ library. However, if i compile the C Project i get a long list of "undefined reference to XY" errors.

Those errors are caused by C++ references:

Errors while compiling:

In function `<c function>':
implicit declaration of function `<function>'
[...]

(list shorted)
Where <c function> is the calling function from C Project and <function> the function from the C++ library.

Errors while linking:

undefined reference to `std::cerr'
undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
[...]
undefined reference to `__gxx_personality_v0'
more undefined references to `__gxx_personality_v0' follow

(lots of errors - example shorted)

In the static C++ library my types / functions are enclosed by extern "C" block:

#ifdef  __cplusplus
extern "C"
{
#endif

    void example();

#ifdef  __cplusplus
}
#endif

To ensure this, all the #include's referring to C++ library are surrounded by such a extern "C" block too.

Compiler: The C++ library is compiled using g++, the C project using gcc - both from MinGW.

CL (Linking): gcc -L<search-path here> -shared [...] -lstdc++ -l<C++ library set here>

Please note the -lstdc++ flag.

Note:



Edit:

Using g++ as linker: undefined reference tostd::cerr'` etc. are gone, but i get another list of errors:

Warning: .drectve `-aligncomm:"___hexdig_D2A",5' unrecognized
undefined reference to `<function / class / method from C++ Library>'
[...]
undefined reference to `_Unwind_Resume'
[...]
undefined reference to `__gxx_personality_v0'
more undefined references to `__gxx_personality_v0' follow
undefined reference to `__chkstk_ms'
[...]

(list shorted)

eg. _Unwind_Resume is available in nm of the c++ library.


Example:

This part is in the Library (C++):

Test.h - Header

#ifndef TEST_H_
#define TEST_H_

// --- Pure C++ here ---
#ifdef __cplusplus

#include <string>     /* (1) */
class Test
{
public:
    Test();


    void aMethod();

private:
    int i;
};
#endif

// --- Mixed (C / C++ ) here ---

#ifdef __cplusplus
extern "C"
{
#endif

    extern void doTest();
    extern void invoke();

#ifdef  __cplusplus
}
#endif

#endif /* TEST_H_ */

Test.cpp - Implementation of C++ part

#include "Test.h"


Test::Test() : i(0) { }

void Test::aMethod()
{
    this->i++;
}


void invoke()
{
    Test i;
    i.aMethod();
}

Test.c - Implementation of C Interface (still in library!)

#include "Test.h"

void doTest()
{
    invoke();
}

Now, here comes the part of the second project (pure C):

#include "Test.h"


int main(int argc, char* argv[])
{
    doTest();

    return 0;
}

(1): If i remove that include i can compile the C Project. But adding one include for any kind of class or c++ stuff causes lots of errors while linking. unfortunately i need to include classes etc..

Btw. here's the error i get for this example:

Test.cpp:(.text+0x1b): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
Test.cpp:(.text+0x64): undefined reference to `_Unwind_Resume'
Test.cpp:(.text$_ZN4TestD1Ev[Test::~Test()]+0x12): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
Test.cpp:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'

setting -lstdc++ flag after (!) the flag for the library i get "only" these:

Test.cpp:(.text+0x64): undefined reference to `_Unwind_Resume'
Test.cpp:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'

解决方案

This problem looks like a bug in MinGW GCC.

  • Using GCC 3.4.5: Error as posted above
  • Using GCC 4.7.2: No Error

Also make sure -lstdc++ is the last (!) of your -l flags.

这篇关于在C项目中使用C ++库导致一长串错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆