如何在C库的实现文件中使用C ++ STL容器?

时间:2022-11-29 02:06:16

Say I wish to use C++ STL containers in the implementation of a library that I want C programs to link to...

假设我希望在我希望C程序链接到的库的实现中使用C ++ STL容器...

My example header is

我的示例标题是

/* mynums.h */
#ifndef MY_NUMS
#define MY_NUMS

#ifdef __cplusplus
extern "C" {
#endif

void append_num(int num);
void print_nums();

#ifdef __cplusplus
}
#endif

#endif

And my example implementation file is

我的示例实现文件是

/* mynums.cpp */
#include "mynums.h"
#include <vector>

using std::vector;

vector<int> nums;

void append_num(int num)
{
    nums.push_back(num);
}

void print_nums()
{
    for (int i = 0; i < nums.size(); i++)
    {
        printf("%d, ", nums[i]);
    }
    printf("\n");
}

My application looks like

我的申请看起来像

/* app.c */
#include "mynums.h"

int main()
{
    append_num(1);
    append_num(2);
    append_num(3);
    print_nums();

    return 0;
}

And my commands to compile these are

我编译这些的命令是

# Compiles and runs
g++ -c -fpic -shared -std=c++0x -o libmynums.so mynums.cpp
g++ -L. -lmynums -o app app.c

# Library compiles, but the application fails
g++ -c -fpic -shared -std=c++0x -o libmynums.so mynums.cpp
gcc -L. -lmynums -o app app.c

The errors I get when I try the second set of compilation commands are those very long stl errors we're oh-so familiar with. One example is

我尝试第二组编译命令时得到的错误是我们非常熟悉的那些非常长的错误。一个例子是

./libmynums.so" In function 'void std:vector<int, std::allocator<int> >::_M_emplace_back_aux<int const&>(int const &)':
mynums.cpp:(.text._ZNSt6vectorIiSaIiEE19_M_emplace_back_auxIIRKiEEEvDpOT_[_ZNSt6vectorIiSaIiEE19_M_emplace_back_auxIIRKiEEEvDpOT_]+0x15d): undefined reference to '__cxa-begin_catch'

I want to be able to compile and link my example application code to the shared object library using gcc. Is this possible? If so, then what changes are necessary to my provided code / commands?

我希望能够使用gcc编译和链接我的示例应用程序代码到共享对象库。这可能吗?如果是这样,那么我提供的代码/命令需要进行哪些更改?

1 个解决方案

#1


8  

The problem is that you're not actually creating a shared library. You're creating an object file and naming it as if it were a shared library.

问题是你实际上并没有创建共享库。您正在创建一个目标文件,并将其命名为共享库。

The -c option to gcc/g++ means to perform the compilation stage only. This results in libmynums.so being an object file. This is why you're able to link to it via g++ but not gcc.

gcc / g ++的-c选项仅表示执行编译阶段。这导致libmynums.so是一个目标文件。这就是为什么你能够通过g ++而不是gcc链接到它的原因。

Remove the -c option when compiling mynums.cpp and you'll get a shared library.

在编译mynums.cpp时删除-c选项,您将获得一个共享库。

#1


8  

The problem is that you're not actually creating a shared library. You're creating an object file and naming it as if it were a shared library.

问题是你实际上并没有创建共享库。您正在创建一个目标文件,并将其命名为共享库。

The -c option to gcc/g++ means to perform the compilation stage only. This results in libmynums.so being an object file. This is why you're able to link to it via g++ but not gcc.

gcc / g ++的-c选项仅表示执行编译阶段。这导致libmynums.so是一个目标文件。这就是为什么你能够通过g ++而不是gcc链接到它的原因。

Remove the -c option when compiling mynums.cpp and you'll get a shared library.

在编译mynums.cpp时删除-c选项,您将获得一个共享库。