# Problems with the CMake files for CFITSIO

**URL:** https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178
**Category:** Code
**Created:** [December 11, 2024, 11:37am UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178 "2024-12-11T11:37:18Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![perdrix](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@perdrix](https://discourse.cmake.org/u/perdrix)
#### Post date: [December 11, 2024, 11:37am UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178/1 "2024-12-11T11:37:18Z")

</div>

Not long ago I pulled down the CFITSIO repository and built it. The exact sequence of commands was:

```auto
#
# Download CFITSIO from <https://heasarc.gsfc.nasa.gov/FTP/software/fitsio/c/cfitsio-4.5.0.tar.gz>
# to your Downloads folder, then untar it, build and install CFITSIO
#
cd ~/Downloads
wget https://github.com/HEASARC/cfitsio/archive/refs/tags/cfitsio4_5_0_20240826.tar.gz
tar -zxvf cfitsio4_5_0_20240826.tar.gz
cd cfitsio-cfitsio4_5_0_20240826
#
# Get the updated CMakeLists.txt
#
rm CMakeLists.txt
wget https://raw.githubusercontent.com/HEASARC/cfitsio/37f72c0ae47a17184bdadb83f83ef5ff1b184f69/CMakeLists.txt
mkdir build
cmake -B build -G Ninja -DBUILD_SHARED_LIBS=OFF -DUSE_PTHREADS=ON -DUSE_BZIP2=OFF
cd build
ninja
sudo ninja install

```

That worked well! Unfortunately when I added the following lines to the CMakeLists.txt file for my project:

```auto
set(cfitsio_DIR "/usr/local/lib/cfitsio-4.5.0/")
find_package(cfitsio 4.5.0 REQUIRED)

```

I received:

```auto
1> [CMake] CMake Error at /usr/local/lib/cfitsio-4.5.0/cfitsio-config.cmake:37 (include):
1> [CMake] include could not find requested file:
1> [CMake] 
1> [CMake] /cfitsio-targets.cmake

```

This is probably because there’s a problem with file cfitsio-config-cmake.in from which cfitsio-config.cmake is built.

I’ve reported this to the HEASARC folks, but right now they are struggling with this problem, saying:

> I think that file was submitted by a user, so it may not be straightforward to get them to sort this out. So far my attempts to get your approach working aren’t succeeding.

They did say that this (to me rather odd) incantation works:

```auto
# Find the library using pkg-config
find_package(PkgConfig REQUIRED)
pkg_check_modules(CFITSIO REQUIRED IMPORTED_TARGET cfitsio)

# Add the include directories and link libraries to the target
add_executable(fpack fpack.c fpackutil.c)
target_link_libraries(fpack PkgConfig::CFITSIO)

```

If anyone who’s much better at cmake than I has time to look at this issue I’d be most grateful, particularly if you can contribute a fix!

Many thanks, David

---

<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: [December 11, 2024, 12:10pm UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178/2 "2024-12-11T12:10:23Z")

</div>

That is most probably because of this line in their [cfitsio-config.cmake.in](https://github.com/HEASARC/cfitsio/blob/3b6fc6feea25c6e8926645e06570b16f6e15cfeb/cfitsio-config.cmake.in#L37):

```auto
include("${CFITSIO_CMAKE_DIR}/cfitsio-targets.cmake")

```

This `CFITSIO_CMAKE_DIR` variable doesn’t seem to be actually initialized to any value, so it resolves to an empty string, resulting in that `/cfitsio-targets.cmake` path, which fails your configuration. Apparently, it should be initialized [here](https://github.com/HEASARC/cfitsio/blob/3b6fc6feea25c6e8926645e06570b16f6e15cfeb/cfitsio-config.cmake.in#L14), but this line is commented out, and I can’t say what was their plan with that.

Usually(?), that `include()` statement would use `CMAKE_CURRENT_LIST_DIR`, so in a regular(?) project that line would look like this:

```auto
include("${CMAKE_CURRENT_LIST_DIR}/cfitsio-targets.cmake")

```

And the workaround they suggested is to use pkg-config, which this project also provides a config for, and that one seemingly has no such problem (_since it works_).

---

<div class="post-metadata">

### Author: ![perdrix](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@perdrix](https://discourse.cmake.org/u/perdrix)
#### Post date: [December 11, 2024, 2:16pm UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178/3 "2024-12-11T14:16:43Z")

</div>

I’m a bit confused tho’ as they already did:

```auto
include("${_prefix}/lib/cmake/cfitsio/cfitsio-targets.cmake")

```

a few lines earlier??  
D.

---

<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: [December 11, 2024, 2:30pm UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178/4 "2024-12-11T14:30:10Z")

</div>

So apparently that one didn’t find the targets file, or it did but there was a “wrong” set of targets provided there, so that later condition `if(NOT TARGET ${LIB_NAME}::${LIB_NAME})` made it fallback to `include("${CFITSIO_CMAKE_DIR}/cfitsio-targets.cmake")`, where it finally failed for good.

In general, it is rarely (_if ever_) a good idea to rely on relative path constructs like `../..`, so I would get rid of that entire block and just go with `CMAKE_CURRENT_LIST_DIR`.

---

<div class="post-metadata">

### Author: ![perdrix](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/a8b319/32.png) [@perdrix](https://discourse.cmake.org/u/perdrix)
#### Post date: [December 12, 2024, 10:29am UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178/5 "2024-12-12T10:29:47Z")

</div>

> [@retif](#):
>
> `include("${CMAKE_CURRENT_LIST_DIR}/cfitsio-targets.cmake")`

The problem probably lies in the cfitsio-targets.cmake file.

The variable LIB\_NAME has no value when executing /usr/local/lib/cfitsio-4.5.0/cfitsio-config.cmake so things are going to go wrong ☹

I added this:

```auto
if(NOT TARGET cfitsio::cfitsio)
    message(FATAL_ERROR "cfitsio::cfitsio is not a TARGET")
endif()

```

and that reported that cfitsio::cfitsio is not a TARGET

---

<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: [December 12, 2024, 10:54am UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178/6 "2024-12-12T10:54:56Z")

</div>

> not setting the “cfitsio::cfitsio” TARGET

That is what I meant by the “wrong” set of targets.

---

<div class="post-metadata">

### Author: ![mj-unseld](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mj-unseld/32/5815_2.png) [@mj-unseld](https://discourse.cmake.org/u/mj-unseld)
#### Post date: [September 19, 2025, 11:01am UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178/7 "2025-09-19T11:01:41Z")

</div>

I do not know, it this is a CFITSIO Issue or a CMake issue. With the CFITSIO 4.6.2 installing and building via CMake, CMake and Ninja and Makefiles seems to be fine. But including it to the project via the CMake find\_package(cfitsio REQUIRED) and target\_link\_libraries(app PRIVATE cfitsio) causes to miss the -lcsfitsio from the linker executeable. I’ve already tried the proposed solutions above leading to that fpack.c and funpack.c are missed. Without the \*.c extensions it’s throwing the same error as before. Maybe I’m missing something. I’ld be happy for some guidance.

---

<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: [September 19, 2025, 11:08am UTC](https://discourse.cmake.org/t/problems-with-the-cmake-files-for-cfitsio/13178/8 "2025-09-19T11:08:52Z")

</div>

So what is the target name in the `cfitsio-targets.cmake`? It is probably not the `cfitsio`, which is what you’ve provided in your `target_link_libraries()`.
