Using pkg-config with Qt Creator/qmake on Mac OSX

2019-01-27 19:32发布

pkg-config doesn't come stantdard with Mac OSX (10.8.4). For my Qt project I wanted to use pkg-config to link in protocol-buffer, so that it would be portable. The very point of choosing Qt was to have a portable app in the first place.

However, qmake would not let me use pkg-config. Linking libraries to a QT project using pkg-config output gives a simple recipe that should work. But it doesn't

with CONFIG += link_pkgconfig PKGCONFIG += protobuf I'm getting the error Project ERROR: Package protobuf not found

Eventhough pkg-config and protobuf are installed using homebrew and in path. And the problem is with all pkg-config packages.

qmaketest$which pkg-config
/usr/local/bin//pkg-config
qmaketest$pkg-config --cflags --libs libssl
-lssl -lcrypto -lz 
qmaketest$cat project.proj 
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = project
TEMPLATE = app


SOURCES += 

HEADERS  += 

FORMS    += 

OTHER_FILES += 

CONFIG += link_pkgconfig
PKGCONFIG += libssl

qmaketest$/Applications/Other/Qt5.0.2/5.0.2/clang_64/bin/qmake project.proj 
Project ERROR: Package libssl not found

When I dug deeper I found a solution.

3条回答
叛逆
2楼-- · 2019-01-27 20:08

Don't create or modify /etc/launchd.conf: a more versatile solution is to create a file in /etc/paths.d that contains the path to add. This can be done using this one-line command:

sudo sh -c 'echo /usr/local/bin > /etc/paths.d/local'

You can also do the following if you installed MacPorts:

sudo sh -c 'echo /opt/local/bin > /etc/paths.d/MacPorts'

You then have to close/reopen the session.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-27 20:13

Solution

Add the line QT_CONFIG -= no-pkg-config to the project file.

Explanation

Support for pkg-config is disabled by default in the Qt package for mac.

So the qmake is configured to assume that there is no pkg-config on the system.

They do that through macro variable QT_CONFIG

qmaketest$grep QT_CONFIG /Applications/Other/Qt5.0.2/5.0.2/clang_64/mkspecs/qconfig.pri QT_CONFIG += minimal-config small-config medium-config large-config full-config build_all debug_and_release no-pkg-config coreservices accessibility opengl shared qpa reduce_exports getaddrinfo ipv6ifname getifaddrs png freetype system-zlib nis cups iconv openssl rpath corewlan concurrent audio-backend v8 v8snapshot debug release qt_framework

So adding a line QT_CONFIG -= no-pkg-config to the project file fixed it.

For Qt Creator

One more problem is that Qt Creator launched by Finder won't have /usr/local/bin in the path. As described in https://serverfault.com/questions/16355/how-to-set-global-path-on-os-x , PATH is getting set somewhere other than launchd.conf and I don't know where and http://overwatering.org/blog/2012/08/setting-path-osx-mountain-lion/ you need to add export PATH=/usr/local/bin:$PATH to /etc/launchd.conf (create the file if it doesn't exist).

Also relaunch Qt Creator after editing the launchd.conf file as pointed out by @vmarquet

查看更多
趁早两清
4楼-- · 2019-01-27 20:18

Step by step, these worked for me (lets called the library "3rdpart") :

  1. Install Brew packet manager : https://brew.sh
  2. Install pkg-config via brew : $ brew install pkg-config
  3. Install your library via brew : $ brew install 3rdpart
  4. Add the following lines in your .pro

    unix {
     CONFIG += link_pkgconfig
     PKGCONFIG += lib3rdpart
    }
    macx {
     # Enable pkg-config (pkg-config is disabled by default in the Qt package for mac)
     QT_CONFIG -= no-pkg-config
     # pkg-config location if your brew installation is standard
     PKG_CONFIG = /usr/local/bin/pkg-config
    }
    

With this method, I managed to compile this Qt Project using Graphviz libraries. You can see the readme file to install or the GraphViz.pri file to have an example of qmake configuration.

查看更多
登录 后发表回答