首页 > 解决方案 > 动态库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)

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

谢谢您的帮助。

标签: c++gccdynamic-linking

解决方案


我需要添加一些选项吗

是的,与图书馆链接。

g++ ... -lmylib

推荐阅读