Search code examples
cunixgccclangld

Generate compiler/linker flags for gcc, given library


Problem

Is there a way I can generate linker/compiler flags for function libraries, given one or more source files?

Details

Software specifications:

  • Operating System: Ubuntu 20.04 LTS x86_64
  • Kernel: 5.11.0-27-generic

Solution

  • There's a handy tool you can use to generate compiler/linker flags for your code. It's called pkg-config, and you can use it like so:

    gcc file.c $(pkg-config --cflags <library>) $(pkg-config --libs <library>)
    

    I'd recommend defining a variable containing all of your dependencies, then using a shell script or other means to generate the pkg-config output for that library.