Search code examples
c++dlllinkereclipse-cdt

undefined reference to 'myNamespace::MyClass::myFunc()'


I am having a problem calling functions from my test_dll.dll in an outside c++ main. Working on eclipse cdt luna sr2 64, Windows 7. Using the MinGW toolchain for compilation.

The dll .cpp code :

#include <iostream>
#include "MyClass.h"

namespace myNamespace {

MyClass::MyClass() :a(1) {
    std::cout << "MyClass():a(" << this->a << ")"<<std::endl;
}

MyClass::~MyClass() { }

void myFunc() {
    std::cout << "myFunc() has been called !" << std::endl;
}

}

and it is compiled within eclipse with the following lines :

g++ -O0 -g3 -Wall -c -fmessage-length=0 -o MyClass.o "..\\MyClass.cpp" 
g++ -shared -o libtest_dll.dll MyClass.o 

My main code is the following :

#include <iostream>
#include "MyClass.h"

using namespace std;

int main() {
    cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
    myNamespace::MyClass *instance = new myNamespace::MyClass;
    instance->myFunc();

    return 0;
}

and it is compiled with the following

g++ "-IC:\\Users\\nxa02192\\Desktop\\MY_WORKSPACE\\test_dll" "-includeC:\\Users\\nxa02192\\Desktop\\MY_WORKSPACE\\test_dll\\MyClass.h" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\test_main.o" "..\\src\\test_main.cpp"
g++ "-LC:\\Users\\nxa02192\\Desktop\\MY_WORKSPACE\\test_dll\\Debug" -o test_main.exe "src\\test_main.o" -llibtest_dll
src\test_main.o: In function `main':
C:\Users\nxa02192\Desktop\MY_WORKSPACE\test_main\Debug/../src/test_main.cpp:20: undefined reference to `myNamespace::MyClass::myFunc()'
collect2.exe: error: ld returned 1 exit status

However, what i don't get is that if i just instanciate the class, it will output the correct a=1 value, as specified in the constructor.

I already specified the include paths to the compiler and the library path and file to the linker, as you can see in the compiling commands. Any Ideas ? Thanks !


Solution

  • Change

    void myFunc() {
        std::cout << "myFunc() has been called !" << std::endl;
    }
    

    to

    void MyClass::myFunc() {
        std::cout << "myFunc() has been called !" << std::endl;
    }