0

I am compiling my code using 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 

I have already included lib using ldconfig command. I need to use -fPIC because eventually this library will be linked with other static libraries to form a dynamic library. When I run this code using the following command

./a.out

It gives "Segment Fault".

code of main.cpp

#include<iostream>
using namespace std;
int main(){
        cout<<"Hello";
}

If I compile this code using g++ main.cpp then code run successfully. I need to compile this code with all lib, so I can use functions of these libs.

I found same problem here : Why does including -fPIC to compile a static library cause a segmentation fault at run time?

But this does not resolve my problem.

Nikesh Joshi
  • 824
  • 6
  • 17

1 Answers1

0

-shared produces a shared object that can be linked with other objects. It does not create an executable. You cannot just execute a shared object. Either link with something else to produce an executable, or don't create a dynamic library in the first place.

Zulan
  • 21,896
  • 6
  • 49
  • 109
  • If you actually need an [executable .so](https://stackoverflow.com/questions/1449987/building-a-so-that-is-also-an-executable), you need to replace (or override) `-shared` with `-pie`. – Alex Che Jul 21 '20 at 19:06