Pkg config windows cmake

FindPkgConfigВ¶

A pkg-config module for CMake.

Finds the pkg-config executable and adds the pkg_get_variable() , pkg_check_modules() and pkg_search_module() commands. The following variables will also be set:

if pkg-config executable was found

pathname of the pkg-config program

version of pkg-config (since CMake 2.8.8)

Checks for all the given modules, setting a variety of result variables in the calling scope.

When the REQUIRED argument is given, the command will fail with an error if module(s) could not be found.

When the QUIET argument is given, no status messages will be printed.

New in version 3.1: The CMAKE_PREFIX_PATH , CMAKE_FRAMEWORK_PATH , and CMAKE_APPBUNDLE_PATH cache and environment variables will be added to the pkg-config search path. The NO_CMAKE_PATH and NO_CMAKE_ENVIRONMENT_PATH arguments disable this behavior for the cache variables and environment variables respectively. The PKG_CONFIG_USE_CMAKE_PREFIX_PATH variable set to FALSE disables this behavior globally.

New in version 3.6: The IMPORTED_TARGET argument will create an imported target named PkgConfig::

Looks for any version of glib2. If found, the output variable GLIB2_VERSION will hold the actual version found.

Looks for at least version 2.10 of glib2. If found, the output variable GLIB2_VERSION will hold the actual version found.

Looks for both glib2-2.0 (at least version 2.10) and any version of gtk2+-2.0. Only if both are found will FOO be considered found. The FOO_glib-2.0_VERSION and FOO_gtk+-2.0_VERSION variables will be set to their respective found module versions.

Requires any version of xrender . Example output variables set by a successful call:

The behavior of this command is the same as pkg_check_modules() , except that rather than checking for all the specified modules, it searches for just the first successful match.

New in version 3.16: If a module is found, the

New in version 3.4.

Retrieves the value of a pkg-config variable varName and stores it in the result variable resultVar in the calling scope.

If pkg-config returns multiple values for the specified variable, resultVar will contain a ;-list .

Variables Affecting BehaviorВ¶

This can be set to the path of the pkg-config executable. If not provided, it will be set by the module as a result of calling find_program() internally.

New in version 3.1: The PKG_CONFIG environment variable can be used as a hint.

New in version 3.1.

Specifies whether pkg_check_modules() and pkg_search_module() should add the paths in the CMAKE_PREFIX_PATH , CMAKE_FRAMEWORK_PATH and CMAKE_APPBUNDLE_PATH cache and environment variables to the pkg-config search path.

If this variable is not set, this behavior is enabled by default if CMAKE_MINIMUM_REQUIRED_VERSION is 3.1 or later, disabled otherwise.

set PKG_CONFIG_PATH in cmake

I have built opencv locally and installed it to a local directory (not the system default ). opencv.pc is present under a folder pkgconfig in this local folder. How can I find this opencv.pc from cmake, because I want to link and include opencv files from my program.

does not have any parameter in which I can force the command to use a specific path (similar to HINTS with find_package() ) and not the system default.

So basically .cmake works:

but I would like to use a opencv.pc located under my/path/to/pkgconfig/opencv.pc .

2 Answers 2

After doing some research and a hint from the @OlivierM, I found the answer.

Here are the steps:

Method I :

CMAKE_PREFIX_PATH can be set to find the .pc files

Читайте также:  Windows не проходит регистрацию

Method II A second method is to use the PKG_CONFIG_PATH , which is a system environment variable to look for .pc files.

Irrespective of which method you use,

For old (traditional) CMake:

Please note that the PKG_OPENCV variable can be named anything. Whatever it is is named, its used as a prefix. For example if you name ABCD , then include directories will be ABCD_INCLUDE_DIRS

The variable PKG_OPENCV_INCLUDE_DIRS and PKG_OPENCV_LIBRARIES contains the header files (compile stage) and libraries (link stage) respectively.

One very important thing I noticed was that the variable PKG_OPENCV_LIBRARIES just provides the libraries and not the library path during the link stage. In order to use the library path as well in one command, one has to use

This variable contains the library path as well as all the libraries listed in the package config file.

For modern CMake:

In modern CMake we don’t want variables, we want targets.

All variables will still be created for backwards compatibility, but IMPORTED_TARGET will create a target you can use in your project which will automatically propagate all of its build and usage requirements:

Cmake not working on Windows — Could NOT find PkgConfig and IlmBase #571

Comments

nranthony commented Nov 28, 2019

Following a fresh start, new vcpkg folder and openvdb clone, I’ve run into errors on the cmake stage. It would appear that it cannot find PkgConfig and IlmBase. See here for cmd line output:
cmake-cmd-output.txt

Specific lines being:

I’m not sure what the PkgConfig refers to. IlmBase was pulled by vcpkg as part of openexr, and in the lib folder all the libraries are 2.3.

Thanks in advance for your time.

The text was updated successfully, but these errors were encountered:

Idclip commented Nov 28, 2019

You don’t need to worry about the PkgConfig message. PkgConfig is a tool which CMake will additionally use to try and find libraries if it’s available. It shouldn’t be necessary with vcpkg.

IIRC, we never ran into IlmBase issues before and were instead stuck with python module issues, is that correct? So this seems new. I also see this CMake warning:

as before, would you mind copy/pasting the CMake command you’re running?

danrbailey commented Nov 28, 2019

That looks like an issue related to trying to use the x86 libraries. I’d recommend double-checking that you’ve set the environment variable correctly. This page has a little information about it:

You can also try including this cmake flag «-DCMAKE_GENERATOR_PLATFORM=x64».

Idclip commented Dec 11, 2019

Hey @nranthony, any luck with the above?

nranthony commented Dec 11, 2019

Hi @Idclip, thanks for the follow up.

I used cmake -DCMAKE_TOOLCHAIN_FILE=

\scripts\buildsystems\vcpkg.cmake .. for the above errors. Following your responses I have been looking back to previous cmake commands, trying to figure out where to start (and alas hindered a little time-wise recently).

I’ll jump in where I left of in the other thread, but any recommendations on a cmake command would be great.

nranthony commented Dec 11, 2019

Hi guys,
I went back and tried a few options of cmake commands that I thought might work. See here for a collated set of commands and outputs:
cmake_cmds_outputs.txt

I first tried where I left off from thread #495, starting with VS2019 and pointing to Python38 (but it looks like it found 3.7.1, so I removed that flag), and then added the -DCMAKE_GENERATOR_PLATFORM=x64 as suggested, and then instead using VS2017.

In all cases it looks like it get’s stuck here:

Thanks in advance for your time. Please let me know if there’s anything else I can provide to help debug.

Idclip commented Dec 11, 2019

Hi @nranthony,
The command you’ve linked shows that you’re requesting the unit tests as part of the build ( -DOPENVDB_BUILD_UNITTESTS=ON ). If you don’t want these, you can simply set this to OFF . If you need these, you also need to install cppunit . If you don’t have this installed, you can do in the same way as you’ve installed the others (#495 (comment))

Читайте также:  Обновление функций до windows 10 версия 20h2 оффлайн

nranthony commented Dec 12, 2019

Cool, thanks for that detail. That allowed me to have a complete cmake, with warnings at the end :

On opening the solution in VS I get an error:
One of more projects in the solution were not loaded correctly.
Please see the Output Window for details.

The Output Window: Some of the properties associated with the solution could not be read.

I tried both VS2017 and VS2019 cmake options, and running VS as administrator in both cases. I tried opening OpenVDB.sln and OpenVDBCore.sln in the openvdb folder.

On trying to build the ALL_BUILD section of code I get an error:
Visual Studio has encountered an unexpected error.

Looking back at the cmake output that appears to work, I still see that it cannot find PkgConfig:

Here’s the last cmake cmd and output for your reference:
cmake_cmd_out.txt

Thanks for your time.

nranthony commented Dec 12, 2019

I understand, and thanks for all your time helping me work through the details.
I’ll aim to post any solutions to the VS issues here for completeness.

I bid you good code.

inhahe commented Jun 5, 2020 •

I’m getting this error too. Maybe it’s not an OPENVDB error per se, but I followed the instructions to build OpenVDB to the T, and my CMake works fine for other projects, so maybe the installation instructions need to be modified..

What is the proper way to use `pkg-config` from `cmake`?

Looking around on the net I have seen a lot of code like this:

However that seems to be the wrong way about doing it, as it only uses the include directories and libraries, but ignored defines, library paths and other flags that might be returned by pkg-config .

What would be the correct way to do this and ensure that all compile and link flags returned by pkg-config are used by the compiled app ? And is there a single command to accomplish this, i.e. something like target_use(app SDL2) ?

6 Answers 6

If you’re using cmake and pkg-config in a pretty normal way, this solution works.

If, however, you have a library that exists in some development directory (such as /home/me/hack/lib), then using other methods seen here fail to configure the linker paths. Libraries that are not found under the typical install locations would result in linker errors, like /usr/bin/ld: cannot find -lmy-hacking-library-1.0 . This solution fixes the linker error for that case.

Another issue could be that the pkg-config files are not installed in the normal place, and the pkg-config paths for the project need to be added using the PKG_CONFIG_PATH environment variable while cmake is running (see other Stack Overflow questions regarding this). Assuming you have the correct pkg-config path set up, this solution also fixes that problem.

The solution boils down to this final version of a working CMakeLists.txt:

Note that target_link_libraries does more than change the linker commands. It also propagates other PUBLIC properties of specified targets like: compiler flags, compiler defines, include paths, etc.

First of, the call:

should be replaced with:

The find_package() call is more flexible and allows options such as REQUIRED , that do things automatically that one would have to do manually with include() .

Secondly, manually calling pkg-config should be avoid when possible. CMake comes with a rich set of package definitions, found in Linux under /usr/share/cmake-3.0/Modules/Find*cmake . These provide more options and choice for the user than a raw call to pkg_search_module() .

As for the mentioned hypothetical target_use() command, CMake already has that built-in in a way with PUBLIC|PRIVATE|INTERFACE. A call like target_include_directories(mytarget PUBLIC . ) will cause the include directories to be automatically used in every target that uses mytarget , e.g. target_link_libraries(myapp mytarget) . However this mechanism seems to be only for libraries created within the CMakeLists.txt file and does not work for libraries acquired with pkg_search_module() . The call add_library(bar SHARED IMPORTED) might be used for that, but I haven’t yet looked into that.

Читайте также:  Служба загрузки файлов windows

As for the main question, this here works in most cases:

The SDL2_CFLAGS_OTHER contains defines and other flags necessary for a successful compile. The flags SDL2_LIBRARY_DIRS and SDL2_LDFLAGS_OTHER are however still ignored, no idea how often that would become a problem.

How to install pkg config in windows?

I am trying to do it, but all I can get is some source code that I don’t know how to do deal with I downloaded from http://pkgconfig.freedesktop.org/releases/ .

8 Answers 8

Download pkg-config and its depend libraries :

This is a step-by-step procedure to get pkg-config working on Windows, based on my experience, using the info from Oliver Zendel’s comment.

I assume here that MinGW was installed to C:\MinGW. There were multiple versions of the packages available, and in each case I just downloaded the latest version.

Now CMake will be able to use pkg-config if it is configured to use MinGW.

A alternative without glib dependency is pkg-config-lite.

Extract pkg-config.exe from the archive and put it in your path.

Nowdays this package is available using chocolatey, then it could be installed whith

I did this by installing Cygwin64 from this link https://www.cygwin.com/ Then — View Full, Search gcc and scroll down to find pkg-config. Click on icon to select latest version. This worked for me well.

  1. Install mingw64 from https://sourceforge.net/projects/mingw-w64/. Avoid program files/(x86) folder for installation. Ex. c:/mingw-w64
  2. Download pkg-config__win64.zip from here
  3. Extract above zip file and copy paste all the files from pkg-config/bin folder to mingw-w64. In my case its ‘C:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin’
  4. Now set path = C:\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin taddaaa you are done.

If you find any security issue then follow steps as well

  1. Search for windows defender security center in system
  2. Navigate to apps & browser control> Exploit protection settings> Program setting> Click on ‘+add program customize’
  3. Select add program by name
  4. Enter program name: pkgconf.exe
  5. OK
  6. Now check all the settings and set it all the settings to off and apply.

I would like to extend the answer of @dzintars about the Cygwin version of pkg-config in that focus how should one use it properly with CMake, because I see various comments about CMake in this topic.

I have experienced many troubles with CMake + Cygwin’s pkg-config and I want to share my experience how to avoid them.

1. The symlink C:/Cygwin64/bin/pkg-config -> pkgconf.exe does not work in Windows console.

It is not a native Windows .lnk symlink and it won’t be callable in Windows console cmd.exe even if you add «.;» to your %PATHEXT% (see https://www.mail-archive.com/cygwin@cygwin.com/msg104088.html).

It won’t work from CMake, because CMake calls pkg-config with the method execute_process() (FindPkgConfig.cmake) which opens a new cmd.exe.

Solution: Add -DPKG_CONFIG_EXECUTABLE=C:/Cygwin64/bin/pkgconf.exe to the CMake command line (or set it in CMakeLists.txt).

2. Cygwin’s pkg-config recognizes only Cygwin paths in PKG_CONFIG_PATH (no Windows paths).

For example, on my system the .pc files are located in C:\Cygwin64\usr\x86_64-w64-mingw32\sys-root\mingw\lib\pkgconfig. The following three paths are valid, but only path C works in PKG_CONFIG_PATH:

  • A) c:/Cygwin64/usr/x86_64-w64-mingw32/sys-root/mingw/lib/pkgconfig — does not work.
  • B) /c/cygdrive/usr/x86_64-w64-mingw32/sys-root/mingw/lib/pkgconfig — does not work.
  • C) /usr/x86_64-w64-mingw32/sys-root/mingw/lib/pkgconfig — works.

Solution: add .pc files location always as a Cygwin path into PKG_CONFIG_PATH.

3) CMake converts forward slashes to backslashes in PKG_CONFIG_PATH on Cygwin.

It happens due to the bug https://gitlab.kitware.com/cmake/cmake/-/issues/21629. It prevents using the workaround described in [2].

Solution: manually update the function _pkg_set_path_internal() in the file C:/Program Files/CMake/share/cmake-3.x/Modules/FindPkgConfig.cmake. Comment/remove the line:

4) CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH have no effect on pkg-config in Cygwin.

Solution: Use only PKG_CONFIG_PATH as an environment variable if you run CMake builds on Cygwin. Forget about CMAKE_PREFIX_PATH, CMAKE_FRAMEWORK_PATH, CMAKE_APPBUNDLE_PATH.

Оцените статью