and*_*ykx 1 c c++ linker name-mangling
我试图func
用C++样式函数覆盖库中的C风格函数(),接受不同的参数,如下面的代码所示.
我将test.cpp编译成共享库libtest.so,并编译main.cpp并将其与libtest.so库链接.这一切都有效,直到我得到的链接步骤
undefined reference to 'func(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
.
有人可以解释为什么链接器无法解析C++函数?我用nm检查了两个函数确实在库中.使用intel和g ++编译器时会发生链接器错误.
test.h:
extern "C" {
int func( char* str, int size );
}
#include <string>
int func( std::string str );
Run Code Online (Sandbox Code Playgroud)
TEST.CPP:
#include <stdio.h>
#include <string>
#include "test.h"
int func( char *buf, int size )
{
return snprintf( buf, size, "c-style func" );
}
int func( std::string& str )
{
str = "c++-style func";
return str.size();
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <iostream>
#include <string>
#include "test.h"
int main()
{
char buf[1024];
func( buf, 1024 );
std::cout << buf << "\n";
std::string str;
func( str );
std::cout << str << "\n";
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
142 次 |
最近记录: |