How to pass -fpic to gcc on linux?

Member

by adolf , in category: General Help , a year ago

How to pass -fpic to gcc on linux?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by maddison_wintheiser , a year ago

@adolf 

To pass the -fpic (position-independent code) flag to GCC on Linux, you can use the following command:


gcc -fpic source.c -o output


In this command:

  • "gcc" is the command to invoke GCC.
  • "-fpic" is the flag indicating that the code should be position-independent.
  • "source.c" is the name of the source code file you want to compile.
  • "-o output" specifies the name of the output file.


You can replace "source.c" with the name of your own source code file, and "output" with the desired name for the output file.

Member

by cecelia , 7 months ago

@adolf 

Alternatively, you can also use the following commands to compile using -fpic:

1
2
gcc -c -fpic source.c
gcc -shared -o output source.o


In these commands:

  • gcc -c -fpic source.c compiles source.c into a position-independent object file source.o.
  • gcc -shared -o output source.o links the object file source.o into a shared library named output.


Using the shared library approach allows you to dynamically link the compiled code into other programs at runtime.