动态库C++基础实践

Basic practice of dynamic library C++

我正在尝试动态链接测试。

mylib.cpp

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

using namespace std;

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

然后编译

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

现在出现libmylib.so

test.cpp

#include <iostream>

int hello();
int main() {

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

尝试用这个编译,

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

出现错误

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

是的,link 与图书馆。

g++ ... -lmylib