C++ 与 -fPIC 的链接问题?段错误
C++ linking issue with -fPIC? Segment fault
我正在使用 g++ 编译我的代码
g++ -g -O3 -fPIC -shared -lstdc++ -std=c++0x -I/home/nikesh.joshi/somepath/ main.cpp /home/nikesh.joshi/ml/somepath_apis/somepath/somefile.o
我已经使用 ldconfig 命令包含了 lib。
我需要使用 -fPIC 因为最终这个库将与其他静态库链接以形成动态库。
当我 运行 此代码使用以下命令时
./a.out
它给出"Segment Fault"。
代码 main.cpp
#include<iostream>
using namespace std;
int main(){
cout<<"Hello";
}
如果我使用 g++ main.cpp 编译此代码,那么 运行 代码会成功。我需要用所有库编译这段代码,这样我才能使用这些库的函数。
我在这里发现了同样的问题:Why does including -fPIC to compile a static library cause a segmentation fault at run time?
但这并不能解决我的问题。
-shared
生成一个可以与其他对象 link 编辑的共享对象。它不会创建可执行文件。您不能只执行共享对象。要么 link 用其他东西生成可执行文件,要么一开始就不要创建动态库。
我正在使用 g++ 编译我的代码
g++ -g -O3 -fPIC -shared -lstdc++ -std=c++0x -I/home/nikesh.joshi/somepath/ main.cpp /home/nikesh.joshi/ml/somepath_apis/somepath/somefile.o
我已经使用 ldconfig 命令包含了 lib。 我需要使用 -fPIC 因为最终这个库将与其他静态库链接以形成动态库。 当我 运行 此代码使用以下命令时
./a.out
它给出"Segment Fault"。
代码 main.cpp
#include<iostream>
using namespace std;
int main(){
cout<<"Hello";
}
如果我使用 g++ main.cpp 编译此代码,那么 运行 代码会成功。我需要用所有库编译这段代码,这样我才能使用这些库的函数。
我在这里发现了同样的问题:Why does including -fPIC to compile a static library cause a segmentation fault at run time?
但这并不能解决我的问题。
-shared
生成一个可以与其他对象 link 编辑的共享对象。它不会创建可执行文件。您不能只执行共享对象。要么 link 用其他东西生成可执行文件,要么一开始就不要创建动态库。