CImg X11 Linking Error using CLion and CMake

試著忘記壹切 提交于 2019-12-07 19:34:49

问题


I have a C++ project in CLion (Ubuntu) that uses the CImg library. Currently, I cannot get the project to build properly. I have included the CImg.h file in my main.cpp file. I get an output like seen here. I made sure I had X11 tools installed by running the following command in the terminal:

sudo apt-get install libx11-dev

In the question linked to above, there is an answer that states to add the following compiler options:

-L/usr/X11R6/lib -lm -lpthread -lX11

However, I run into 2 problems with this. First, I am using CMake in CLion, not the command window, so I am not sure how to use this command (assuming it works). Secondly, the path specified above does not seem to exist on my computer - there is no X11R6 folder within /usr. Therefore, how can I properly link the required X11 libraries so I may begin to use CImg?

(Also, I don't know if this is relevant, but I do not have Magick++ installed.)


回答1:


The way CImg programs need to be built is sadly not abundantly clear from the documentation. Here is my take on it:

Step 1 - Do you need to display images on the screen?

No, don't laugh, you may be manipulating images on a headless web server.

  • If you do NOT need to display images, you must do #define cimg_display 0 before you include "CImg.h"
  • If you do need to display images, and you are on Windows, you need to link with gdi32
  • If you do need to display images and you are on macOS, OSX, or Unix/Linux, you need to set the header include path for, and link with, X11

Step 2 - Do you need to read/write PNG format images?

  • If you do NOT need this functionality, or if you are happy to let CImg use your already-installed ImageMagick to read/write PNG images, do nothing.

  • If you want to use CImg's built-in ability to read/write PNG images, you need to #define cimg_use_png 1 before you include "CImg.h", and you need to set the header include path for libpng and link with both libpng and libz.

Step 3 - Do you need to read/write JPEG format images?

  • If you do NOT need this functionality, or if you are happy to let CImg use your already-installed ImageMagick to read/write JPEG images, do nothing.

  • If you want to use CImg's built-in ability to read/write JPEG images, you need to #define cimg_use_jpeg 1 before you include "CImg.h", and you need to set the header include path for libjpeg and link with libjpeg.

Step 4 - Other stuff

One further thing is that you may need to link with the maths and the POSIX threads library (using -lm -lpthread as linker switches), but I am not sure how to check that across all platforms, so I'll leave that to you - it can probably do no harm to add it.


So, bearing all that in mind, and using this little test program:

#include "CImg.h"

using namespace cimg_library;

int main() {
    CImg<unsigned char> img(640,400,1,3);         // Define a 640x400 color image with 8 bits per color component.
    img.fill(0);                                  // Set pixel values to 0 (color : black)
    unsigned char purple[] = { 255,0,255 };       // Define a purple color
    img.draw_text(100,100,"Hello World",purple);  // Draw a purple "Hello world" at coordinates (100,100).
    img.display("Window Title");                  // Display the image in a display window.
    img.save_png("test.png");                     // Save as PNG to prove we linked correctly
    img.save_jpeg("test.jpg");                    // Save as JPG to prove we linked correctly
    return 0;
}

Your CMake CmakeLists.txt inside CLion will be something like this:

cmake_minimum_required(VERSION 3.6)
project(CImgProject)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(CImgProject ${SOURCE_FILES})

# You can alter these according to your needs, e.g if you don't need to display images - set(YOU_NEED_X11 0)
set(YOU_NEED_X11 1)
set(YOU_NEED_PNG 1)
set(YOU_NEED_JPG 1)

if(${YOU_NEED_X11} EQUAL 1)
    message(STATUS "Looking for X11...")
    find_package(X11 REQUIRED)
    include_directories(${X11_INCLUDE_DIR})
    target_link_libraries(CImgProject ${X11_LIBRARIES})
else()
    target_compile_definitions(CImgProject PRIVATE cimg_display=0)
endif()

if(${YOU_NEED_JPG} EQUAL 1)
    message(STATUS "Looking for libjpg...")
    find_package(JPEG REQUIRED)
    include_directories(${JPEG_INCLUDE_DIR})
    target_link_libraries (CImgProject ${JPEG_LIBRARY})
    target_compile_definitions(CImgProject PRIVATE cimg_use_jpeg=1)
endif()

if(${YOU_NEED_PNG} EQUAL 1)
    message(STATUS "Looking for libpng...")
    find_package(PNG REQUIRED)
    include_directories(${PNG_INCLUDE_DIR})
    target_link_libraries (CImgProject ${PNG_LIBRARY})
    target_compile_definitions(CImgProject PRIVATE cimg_use_png=1)
endif()

Here is my, rather disappointing, image after all that!




回答2:


The other answer did not work for me. However, adding

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(My_Project Threads::Threads)

target_link_libraries(My_Project X11)

to my CmakeLists.txt worked to run the code from the link below from CLion. I'm not experienced with CMake, so I can't explain it well (and cannot guarantee that it is optimal, but it does work). I figured it out by seeing which flags were set in the instructions (http://cimg.eu/reference/group__cimg__overview.html), figuring out which ones were unnecessary, and then figuring out how to add the necessary two (-lpthread and -lX11) to the CMakeLists file.

This is not a complete install solution; it adds what the other answers do not say. Before doing the above, don't forget to

sudo apt-get install libx11-dev

and

sudo apt-get install python3-tk


来源:https://stackoverflow.com/questions/38903996/cimg-x11-linking-error-using-clion-and-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!