I'm making a C++ Shared Library and when I compile a main exe that uses the library the compiler gives me:
main.cpp:(.text+0x21): undefined reference to `FooClass::SayHello()' collect2: ld returned 1 exit status
Library code:
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"; }
Compiling with:
g++ -shared -fPIC fooclass.cpp -o libfoo.so
Main: 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; }
Compiling with:
g++ -I. -L. -lfoo main.cpp -o main
The machine is an Ubuntu Linux 12
Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With