whitebear
whitebear

Reputation: 12423

Basic practice of dynamic library C++

I am trying dynamic-linking test.

mylib.cpp

#include<iostream>
#include<stdio.h>

using namespace std;

int hello(){
   cout<<"Hello,World!"<<endl;
   return 1;
}

then compile

g++ -shared -o libmylib.so mylib.cpp

Now there appears the libmylib.so

test.cpp

#include <iostream>

int hello();
int main() {

    hello();
    std::cout << "Test Finish!\n";
    return 0;
}

Try to compile with this,

g++ -o test test.cpp -L ./

There comes the error

Undefined symbols for architecture x86_64:
  "hello()", referenced from:
      _main in test-37bd2a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Do I need to add some options or are there something wrong in my source code??

Thank you for your help.

Upvotes: 0

Views: 47

Answers (1)

KamilCuk
KamilCuk

Reputation: 140860

Do I need to add some options

yes, link with the library.

g++ ... -lmylib

Upvotes: 3

Related Questions