How to pass -fpic to gcc on linux?


by adolf , in category: General Help , 7 months ago

How to pass -fpic to gcc on linux?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by maddison_wintheiser , 7 months ago


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.


by cecelia , 2 months ago


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

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.