0

I'm trying to use libjpeg in my C++ project that's built using CMake on macOS. Here are the steps I took to try and use it.

$ brew install libjpeg

In CMakeLists.txt

find_package (jpeg REQUIRED)
target_link_libraries(my_project libjpeg)

In main.cpp

#include <jpeglib.h>

To build

$ cmake -Bbuild -G Xcode .

When I build and run on Xcode, I get the error jpeglib.h file not found.

I have verified that this file exists in /usr/local/include.

Thanks for any help!

EDIT: This is clearly not a duplicate of the linked question, as we are dealing with two completely different build systems. I'm not using gcc directly and instead using cmake. This is specifically a cmake issue.

user11909399
  • 49
  • 1
  • 7
  • Possible duplicate of [On mac, g++ (clang) fails to search /usr/local/include and /usr/local/lib by default](https://stackoverflow.com/questions/23905661/on-mac-g-clang-fails-to-search-usr-local-include-and-usr-local-lib-by-def) – krisz Aug 11 '19 at 00:58
  • Does cmake find `jpeg` or not? – KamilCuk Aug 11 '19 at 08:48

2 Answers2

1
target_link_libraries(my_project libjpeg)

You use find_package like this:

target_link_libraries(my_project ${JPEG_LIBRARIES})
target_add_include_directories(my_project ${JPEG_INCLUDE_DIR})
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
0

Here's what your CMakeLists.txt should look like

find_package (jpeg REQUIRED)
target_link_libraries(my_project jpeg::jpeg)

When you run the CMake commands, the find_package calls should output if that succeeded. Look out for those 'Found:' outputs lines to make sure that the jpeg library is being found.

If it's not being found, then it likely doesn't have anything to do with your CMakeLists.txt because the find_package() call looks correct. If the cmake file says libjpeg wasn't found, try looking into if it's being installed correctly.

vpaladino778
  • 157
  • 14