简体   繁体   English

在Mac OSX上使用pkg-config和Qt Creator / qmake

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

pkg-config doesn't come stantdard with Mac OSX (10.8.4). 对于Mac OSX(10.8.4),pkg-config并没有出现。 For my Qt project I wanted to use pkg-config to link in protocol-buffer, so that it would be portable. 对于我的Qt项目,我想使用pkg-config链接协议缓冲区,以便它可以移植。 The very point of choosing Qt was to have a portable app in the first place. 选择Qt的关键在于首先要有一个便携式应用程序。

However, qmake would not let me use pkg-config. 但是,qmake不会让我使用pkg-config。 Linking libraries to a QT project using pkg-config output gives a simple recipe that should work. 使用pkg-config输出将库链接到QT项目提供了一个应该有效的简单配方。 But it doesn't 但事实并非如此

with CONFIG += link_pkgconfig PKGCONFIG += protobuf I'm getting the error Project ERROR: Package protobuf not found 使用CONFIG += link_pkgconfig PKGCONFIG += protobuf我收到错误Project ERROR: Package protobuf not found

Eventhough pkg-config and protobuf are installed using homebrew and in path. 尽管pkg-config和protobuf是使用自制程序和路径安装的。 And the problem is with all pkg-config packages. 问题出在所有pkg-config软件包上。

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. 当我深入挖掘时,我找到了解决方案。

Solution

Add the line QT_CONFIG -= no-pkg-config to the project file. QT_CONFIG -= no-pkg-config行添加到项目文件中。

Explanation 说明

Support for pkg-config is disabled by default in the Qt package for mac. 默认情况下,在Mac的Qt包中禁用对pkg-config的支持。

So the qmake is configured to assume that there is no pkg-config on the system. 因此,qmake配置为假设系统上没有pkg-config。

They do that through macro variable QT_CONFIG 他们通过宏变量QT_CONFIG做到这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 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. 因此,在项目文件中添加一行QT_CONFIG -= no-pkg-config修复它。

For Qt Creator 对于Qt Creator

One more problem is that Qt Creator launched by Finder won't have /usr/local/bin in the path. 另一个问题是Finder启动的Qt Creator在路径中不会有/ usr / local / bin。 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). https://serverfault.com/questions/16355/how-to-set-global-path-on-os-x中所述PATH正在设置在launchd.conf以外的地方,我不知道在哪里http ://overwatering.org/blog/2012/08/setting-path-osx-mountain-lion/您需要将导出PATH = / usr / local / bin:$ PATH添加到/etc/launchd.conf(创建文件)如果它不存在)。

Also relaunch Qt Creator after editing the launchd.conf file as pointed out by @vmarquet 在编辑了@vmarquet指出的launchd.conf文件后重新启动Qt Creator

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. 不要创建或修改/etc/launchd.conf:一个更通用的解决方案是在/etc/paths.d中创建一个包含要添加的路径的文件。 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: 如果安装了MacPorts,也可以执行以下操作:

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

You then have to close/reopen the session. 然后,您必须关闭/重新打开会话。

Step by step, these worked for me (lets called the library "3rdpart") : 一步一步,这些对我有用(我们称之为库“3rdpart”):

  1. Install Brew packet manager : https://brew.sh 安装Brew数据包管理器: https//brew.sh
  2. Install pkg-config via brew : $ brew install pkg-config 通过brew安装pkg-config:$ brew install pkg-config
  3. Install your library via brew : $ brew install 3rdpart 通过brew安装您的库:$ brew install 3rdpart
  4. Add the following lines in your .pro 在.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. 使用此方法,我设法使用Graphviz库编译此Qt项目 You can see the readme file to install or the GraphViz.pri file to have an example of qmake configuration. 您可以看到要安装的自述文件GraphViz.pri文件以获得qmake配置的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM