How to use PKG_CONFIG on my application?

Hi all,

When I try to build up my application tool which has dependence on libpulse.
So, I would like use PKG_CONFIG on my Makefile to link correct libraries.
My Makefile is

# define external ld flag
PLATFORM_FLAGS:=                                                      
PLATFORM_LD_FLAGS:=$(shell $(PKG_CONFIG) --libs --static libpulse) -lpulse-simple -lpthread -lm
BUILD_FLAGS:=                                                                               
BUILD_OPTIONS:=                                                            
              
define Build/Compile
    $(MAKE) -C $(PKG_BUILD_DIR) \
        CC="$(TARGET_CC)" \      
        CXX="$(TARGET_CXX)" \
        CFLAGS="$(TARGET_CFLAGS) $(TARGET_CPPFLAGS) -Wall $(PLATFORM_FLAGS) $(BUILD_FLAGS)" \
        LDFLAGS="$(TARGET_LDFLAGS) $(PLATFORM_LD_FLAGS)" \                                                        
        $(BUILD_OPTIONS) all                              
endef       

$(shell $(PKG_CONFIG) --libs --static libpulse) did not work.
How can I use PKG_CONFIG correctly and pass results into src/Makefile?
Please help. Thanks.

Two ways

By default, it'll call ./configure in the build directory (unless you tell it different, see below). If your package uses ./configure, you can pass variables and arguments to it by the CONFIGURE_VARS and CONFIGURE_ARGS

CONFIGURE_VARS are environmental flags, CONFIGURE_ARGS is for arguments.

Example from my rust package:

CONFIGURE_VARS += \
	CARGO_HOME=$(BUILD_DIR_HOST)/.cargo \
	RUSTUP_HOME=$(BUILD_DIR_HOST)/.rustup \
 	ac_cv_path_CARGO="$(BUILD_DIR_HOST)/.cargo/bin/cargo" \
  	ac_cv_path_RUSTC="$(BUILD_DIR_HOST)/.cargo/bin/rustc"

CONFIGURE_ARGS = \
   --prefix="/usr" \
   --sysconfdir="/etc" \
   --enable-nfqueue \
   --localstatedir="/var" \
   --enable-gccprotect \
   --enable-debug \
   --enable-pie \
   --host=$(RUSTC_TARGET_ARCH)

Second way, if your package doesn't use ./configure or needs custom commands, you use define Build/Configure:

You can use $(call Build/Configure/Default) inside it to call the default ./configure routine. You could use this if you needed to setup an environment prior to calling ./configure (like having to call ./bootstrap or something similar)

An example, where the package doesn't use ./configure:

define Build/Configure
	CARGO_HOME=$(BUILD_DIR_HOST)/.cargo \
	RUSTUP_HOME=$(BUILD_DIR_HOST)/.rustup \
	$(BUILD_DIR_HOST)/$(PKG_SOURCE_SUBDIR)/rustup-init.sh -y -v $(RUSTUP_INIT_ARGS)
endef
1 Like

Hi Grommish,

Thanks. Since my package did not have configure file and we don't want to run pkg_config in src/Makefile. Is it possible to create PLATFORM_LD_FLAGS with the result of pkg_config and the PLATFORM_LD_FLAGS is announced out of the Build/xxx function, just like CONFIGURE_VARS? Thanks.

I'm not sure I understand what you are trying to do.

If you had to do what you need to do via a CLI, what would you need to do (and what directories, etc).

If you can give me that, I can maybe tell you how to do it.

Hi Gormmish,

Thanks. Please check this package, it is an example. https://we.tl/t-5oyGT9F8Ih

What I want to do is simple. I want to prepare all parameters on Makefile and pass it to src/Makefile. You can see I try to add library to PLATFORM_LD_FLAGS, but I want the result is coming from pkg-config.

Thanks for your kindly help.

Hi Grommish,

Any update? Thanks.

I've been fighting with package issues myself, so I've not been able to look at it. You can put

LD_FLAGS += \
    -lpulse-simple \
    -lpthread \
    -lm

I'm not sure what/where $(PKG_CONFIG) is coming from though.

PKG_CONFIG_PATH
PKG_CONFIG_LIBDIR

are the only PKG_CONFIG* in package.mk, and $(PKG_CONFIG) I can't find at all.

Hi Grommish,

Thanks. PKG_CONFIG is new definition in my env, sorry about that.
I will double check how to use LD_FLAGS. Thanks. Have a good day.

TARGET_CFLAGS  += 
TARGET_LDFLAGS += 

Add these to your makefile and place what you need there.

That will append it everywhere it's needed.

1 Like

Hi Grommish,

At rule.mk.

PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config

export PKG_CONFIG

That is a binary.

There is a pkg-config you can add to your DEPENDS if you need it, but I think that it's used by the build system itself. I'm not actually at my build system at the moment, but I'm still not sure what you are trying to actually do, and I apologize.

If you need to utilize pkg-config in your package, add a DEPENDS=+pkg-config to the package info.

I don't know if I'm completely missing what you are trying to do (always a possibility), but are you sure you need to call pkg-config directly?

If you are trying to pass CFLAGS or LD_FLAGS, the TARGET_CFLAG/TARGET_LDFLAGS will add them to the make calls in Build/Compile (and Build/Configure, as well, I believe)

1 Like