我正在制作一个 C++ 共享库,当我编译一个使用该库的主 exe 时,编译器给了我:
main.cpp:(.text+0x21): undefined reference to `FooClass::SayHello()'
collect2: ld returned 1 exit status
库代码:
fooclass.h
#ifndef __FOOCLASS_H__
#define __FOOCLASS_H__
class FooClass
{
public:
char* SayHello();
};
#endif //__FOOCLASS_H__
fooclass.cpp
#include "fooclass.h"
char* FooClass::SayHello()
{
return "Hello Im a Linux Shared Library";
}
编译:
g++ -shared -fPIC fooclass.cpp -o libfoo.so
主要:main.cpp
#include "fooclass.h"
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
FooClass * fooClass = new FooClass();
cout<< fooClass->SayHello() << endl;
return 0;
}
编译:
g++ -I. -L. -lfoo main.cpp -o main
该机器是 Ubuntu Linux 12
谢谢!