# Help with pkg\_check\_modules

**URL:** https://discourse.cmake.org/t/help-with-pkg-check-modules/11075
**Category:** Code
**Created:** [June 21, 2024, 4:15pm UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075 "2024-06-21T16:15:08Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![wearyhacker](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wearyhacker/32/4686_2.png) [@wearyhacker](https://discourse.cmake.org/u/wearyhacker)
#### Post date: [June 21, 2024, 4:15pm UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/1 "2024-06-21T16:15:08Z")

</div>

I am trying to use pkg\_check\_modules to locate the sndfile library module on Linux. The library has an entry in the pkg-config database and the log shows that the sndfile module has been found.

```auto
Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2")
 Checking for module 'sndfile'
   Found sndfile, version 1.0.31

```

However the sndfile\_FOUND property is not set. The cache does contain some sndfile related entries.

```auto
pkgcfg_lib_REQUIRED_sndfile:FILEPATH=/usr/lib/x86_64-linux-gnu/libsndfile.so
REQUIRED_LDFLAGS:INTERNAL=-L/usr/lib/x86_64-linux-gnu;-lsndfile
REQUIRED_LIBRARIES:INTERNAL=sndfile
REQUIRED_MODULE_NAME:INTERNAL=sndfile
REQUIRED_STATIC_LDFLAGS:INTERNAL=-L/usr/lib/x86_64-linux-gnu;-lsndfile;-lFLAC;-lm;-lvorbisenc;-lvorbis;-lm;-logg;-lopus;-lm
REQUIRED_STATIC_LIBRARIES:INTERNAL=sndfile;FLAC;m;vorbisenc;vorbis;m;ogg;opus;m
REQUIRED_sndfile_INCLUDEDIR:INTERNAL=
REQUIRED_sndfile_LIBDIR:INTERNAL=
REQUIRED_sndfile_PREFIX:INTERNAL=
REQUIRED_sndfile_VERSION:INTERNAL=
__pkg_config_arguments_REQUIRED:INTERNAL=sndfile
//ADVANCED property for variable: pkgcfg_lib_REQUIRED_sndfile
pkgcfg_lib_REQUIRED_sndfile-ADVANCED:INTERNAL=1

```

I have made a minimal test script.

```auto
cmake_minimum_required(VERSION 3.1)
project(cmt)
find_package(PkgConfig REQUIRED)

pkg_check_modules(REQUIRED IMPORTED_TARGET sndfile)
#pkg_check_modules(REQUIRED sndfile)
message(STATUS "sndfile_FOUND ${sndfile_FOUND}")
message(STATUS "sndfile_LIBRARIES ${sndfile_LIBRARIES}")
message(STATUS "sndfile_LINK_LIBRARIES ${sndfile_LINK_LIBRARIES}")
message(STATUS "sndfile_LIBRARY_DIRS ${sndfile_LIBRARY_DIRS}")
message(STATUS "sndfile_LDFLAGS ${sndfile_LDFLAGS}")
message(STATUS "sndfile_LDFLAGS_OTHER ${sndfile_LDFLAGS_OTHER}")
message(STATUS "sndfile_INCLUDE_DIRS ${sndfile_INCLUDE_DIRS}")
message(STATUS "sndfile_CFLAGS ${sndfile_CFLAGS}")
message(STATUS "sndfile_CFLAGS_OTHER ${sndfile_CFLAGS_OTHER}")

```

What am I doing wrong?  
It has been a number of years since I last used CMake. All help gratefully received.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [June 21, 2024, 6:28pm UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/2 "2024-06-21T18:28:21Z")

</div>

Why using `PkgConfig` if a `cmake export package` exists?

```auto
bash-5.2$ tree /usr/local/Cellar/libsndfile/1.2.2/lib/cmake/SndFile/
/usr/local/Cellar/libsndfile/1.2.2/lib/cmake/SndFile/
|-- SndFileConfig.cmake
|-- SndFileConfigVersion.cmake
|-- SndFileTargets-release.cmake
`-- SndFileTargets.cmake

1 directory, 4 files
bash-5.2$ 

```

Dit you read the manual page [pkg\_check\_modules](https://cmake.org/cmake/help/latest/module/FindPkgConfig.html#command:pkg_check_modules)?

```cmake
find_package(SndFile 1.2 REQUIRED)
if(SndFile_FOUND)
  find_package(PkgConfig REQUIRED)
  pkg_check_modules(sndfile REQUIRED IMPORTED_TARGET "sndfile>=1.2")
  message(STATUS "sndfile_FOUND ${sndfile_FOUND}")
endif()

```

Both work on my host.

---

<div class="post-metadata">

### Author: ![wearyhacker](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wearyhacker/32/4686_2.png) [@wearyhacker](https://discourse.cmake.org/u/wearyhacker)
#### Post date: [June 21, 2024, 8:22pm UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/3 "2024-06-21T20:22:15Z")

</div>

The version of sndfile on ubuntu is from the deb package libsndfile1.  
The package does not include any of the cmake files.

It also has a huge number of reverse dependencies, so it is difficult to replace.

This looks like a real catch 22.

Yes, I have read (TFM) 🙂

---

<div class="post-metadata">

### Author: ![wearyhacker](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wearyhacker/32/4686_2.png) [@wearyhacker](https://discourse.cmake.org/u/wearyhacker)
#### Post date: [June 22, 2024, 8:38am UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/4 "2024-06-22T08:38:48Z")

</div>

I am out of my depth here. I have never used the output of a cmake export command. My last use of cmake was version 2.

The sndfile1 package on ubuntu does not install a cmake find script or an export file.

Any ideas on where I go from here?

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [June 22, 2024, 12:21pm UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/5 "2024-06-22T12:21:34Z")

</div>

> [@ClausKlein](#):
>
> `pkg_check_modules(sndfile REQUIRED IMPORTED_TARGET "sndfile")`

should work, or not?

---

<div class="post-metadata">

### Author: ![wearyhacker](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wearyhacker/32/4686_2.png) [@wearyhacker](https://discourse.cmake.org/u/wearyhacker)
#### Post date: [June 22, 2024, 5:53pm UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/6 "2024-06-22T17:53:18Z")

</div>

Quotes make no differrence ☹

Am I correct in assuming that I have go to the original libsndfile source and do something with CMake build files in there to create the export file?

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [June 23, 2024, 8:59am UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/7 "2024-06-23T08:59:34Z")

</div>

From what I know about pkg-config and CMake, in the case of absent CMake configs, one should be able to find the package with [pkg\_check\_modules()](https://cmake.org/cmake/help/latest/module/FindPkgConfig.html#command:pkg_check_modules), which is what you did (_although with a bit different syntax, as it seems?_). So I am not sure why `sndfile_FOUND` ends up not being set, especially that all the other variables seem to be set.

Moreover, you should get an error if `pkg_check_modules()` doesn’t find the package, as you’ve provided `REQUIRED` argument, but you don’t get an error, do you?

Are there any other `*_FOUND` variables set after your project configuration is done? Maybe it sets some other variable for some reason? And also if you are not using the latest CMake version, I would try the latest one, just for the sake of experiment.

> I have go to the original libsndfile source and do something with CMake build files

That would be my recommendation, but that’s because I’m used to CMake configs and I have never used pkg-config. So yes, if those CMake configs aren’t provided in the package that you get, then the only(?) way to get them would be for you to build that package yourself from sources (_and probably patch its project to add a proper installation procedure, which would create those configs_). Or you can probably just write your own `Findsndfile.cmake` [module](https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html#find-modules). Here’s actually [one example](https://github.com/KDE/k3b/blob/master/cmake/modules/FindSndfile.cmake), which seemingly uses `pkg_check_modules()` underneath (_with some strange `_pc_SNDFILE` variable?_).

However, again, like I said, my understanding is that `pkg_check_modules()` should be able to do the job of discovering the package, so you should be able to rely on that one instead of CMake configs. Just need to find out why it is failing in this particular case.

---

<div class="post-metadata">

### Author: ![wearyhacker](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wearyhacker/32/4686_2.png) [@wearyhacker](https://discourse.cmake.org/u/wearyhacker)
#### Post date: [June 23, 2024, 9:55am UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/8 "2024-06-23T09:55:55Z")

</div>

Just tried the Findsndfile.cmake you found.

It produced the following output

cmake-3.25/Modules/FindPackageHandleStandardArgs.cmak  
e:438 (message):

The package name passed to  
`find_package_handle_standard_args` (Sndfile)  
does not match the name of the calling package (sndfile).  
This can lead to  
problems in calling code that expects `find_package` result variables  
(e.g., `_FOUND`) to follow a certain pattern.  
Call Stack (most recent call first):  
/usr/share/cmake-3.25/Modules/Findsndfile.cmake:33  
(FIND\_PACKAGE\_HANDLE\_STANDARD\_ARGS)  
CMakeLists.txt:5 (find\_package)  
This warning is for project developers. Use -Wno-dev to suppress it.  
Configuring done

The following variable are set in CMakeCache.txt, same as before.

SNDFILE\_LIBRARIES:FILEPATH=/usr/lib/x86\_64-linux-gnu/libsndfile.sopkgcfg\_lib\_\_pc\_SNDFILE\_sndfile:FILEPATH=/usr/lib/x86\_64-linux-gnu/libsndfile.so

FIND\_PACKAGE\_MESSAGE\_DETAILS\_Sndfile:INTERNAL=[/usr/include][/usr/lib/x86\_64-linux-gnu/libsndfile.so][v()]

\_\_pkg\_config\_arguments\_\_pc\_SNDFILE:INTERNAL=sndfile  
\_pc\_SNDFILE\_LDFLAGS:INTERNAL=-L/usr/lib/x86\_64-linux-gnu;-lsndfil \_pc\_SNDFILE\_LIBRARIES:INTERNAL=sndfile

\_pc\_SNDFILE\_MODULE\_NAME:INTERNAL=sndfile

\_pc\_SNDFILE\_STATIC\_LDFLAGS:INTERNAL=-L/usr/lib/x86\_64-linux-gnu;- sndfile;-lFLAC;-lm;-lvorbisenc;-lvorbis;-lm;-logg;-lopus;-lm  
\_pc\_SNDFILE\_STATIC\_LIBRARIES:INTERNAL=sndfile;FLAC;m;vorbisenc;vorbis;m;ogg;opus;m

\_pc\_SNDFILE\_sndfile\_INCLUDEDIR:INTERNAL=

\_pc\_SNDFILE\_sndfile\_LIBDIR:INTERNAL=

\_pc\_SNDFILE\_sndfile\_PREFIX:INTERNAL=

\_pc\_SNDFILE\_sndfile\_VERSION:INTERNAL=  
//ADVANCED property for variable: pkgcfg\_lib\_\_pc\_SNDFILE\_sndfile  
pkgcfg\_lib\_\_pc\_SNDFILE\_sndfile-ADVANCED:INTERNAL=1

I do not know if the warning is significant. I am now wondering if this is something to do the debian/ubuntu multiarch library paths.

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [June 23, 2024, 12:06pm UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/9 "2024-06-23T12:06:24Z")

</div>

I linked that file just as an example of how you could make your own module. I didn’t really expect that particular one to actually work 🙂  
But the warning you got might be still useful, so perhaps you could try different variants of the package name casing with your original project file too.

Anyway, so there are no `*_FOUND` variables set after the project is configured? Perhaps you can try to check them explicitly, like so:

```auto
message(STATUS "sndfile_FOUND ${sndfile_FOUND}")
message(STATUS "SNDFILE_FOUND ${SNDFILE_FOUND}")
message(STATUS "Sndfile_FOUND ${Sndfile_FOUND}")
message(STATUS "SndFile_FOUND ${SndFile_FOUND}")
# etc

```

And you haven’t confirmed that there was no error on your `pkg_check_modules()` with `REQUIRED`.

---

<div class="post-metadata">

### Author: ![wearyhacker](https://discourse.cmake.org/user_avatar/discourse.cmake.org/wearyhacker/32/4686_2.png) [@wearyhacker](https://discourse.cmake.org/u/wearyhacker)
#### Post date: [June 23, 2024, 12:52pm UTC](https://discourse.cmake.org/t/help-with-pkg-check-modules/11075/10 "2024-06-23T12:52:09Z")

</div>

You got it in one.

As expected, stupid mistake on my part.

pkg\_check\_modules(sndfile REQUIRED IMPORTED\_TARGET sndfile)

I know I started with off with sndfile as the first parameter and libsndfile as the last one. But at somehow lost it along the way.

Doh
