c++ - 编译共享库linux gcc/g++ cpp

标签 c++ linux gcc shared-libraries ld

我已经成功编译并创建了一个共享对象,但我不确定它是否真正共享并链接/模拟到 DLL。你能告诉我它是否正确吗? 我有 shared.h、main.cpp、nameOfLib.cpp。

shared.h

#ifndef SHARED_H
   #define SHARED_H
   int helpingLib();
#endif

main.cpp

#include "shared.h"
#pragma once

int main(){
 helpingLib();
 return 0;
}

nameOfLib.cpp

#pragma once
int helpingLib(){ return 20;}

Console:
g++ -shared -fPIC nameOfLib.cpp -o renameTo.so
//create local var 
export $MY_LD_PATH = /home/student/Documents/Total/LinuxXlib/renameTo.so
g++ main.cpp -Wl,$MY_LD_PATH -o executable

首先,它是一个真正可共享的图书馆吗? 即使在 nameOfLib.cpp 中使用复杂的 cpp 代码,它也能正常工作

最佳答案

I'm not sure if it is truly shared

是的,是的。

但是您链接到此共享库的命令行是错误的。改为这样做:

g++ main.cpp -o executable /home/student/Documents/Total/LinuxXlib/renameTo.so

关于c++ - 编译共享库linux gcc/g++ cpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47660266/

相关文章:

c++ - 使用 boost::filesystem::last_write_time 获取锁定文件夹的修改时间

linux - 范围 : bytes 0- FAILS in Apache 2. 2.23

c++ - 使用G++编译C++程序时出错

c++ - 为什么参数不是常量表达式?

c++ - 错误 : no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'std::vector<double>' )

c++ - C++ 计算元音

linux - Ubuntu中 `/var/www`文件夹的默认权限

c - GCC - 标签地址返回当前 EIP 而不是真实标签地址

c++ - 交换 vector 的值和索引

c++ - Linux 守护进程不工作