动态库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)

我需要添加一些选项还是我的源代码有问题??

感谢您的帮助。

解决方法

我需要添加一些选项吗

是的,链接到图书馆。

g++ ... -lmylib