使用CLion和CMake的CImg X11链接错误 [英] CImg X11 Linking Error using CLion and CMake

查看:213
本文介绍了使用CLion和CMake的CImg X11链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CLion(Ubuntu)中有一个使用CImg库的C ++项目.目前,我无法使项目正确构建.我已经将CImg.h文件包含在main.cpp文件中.我在此处看到了类似的输出.通过在终端中运行以下命令,确保已安装X11工具:

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

但是,我遇到了两个问题.首先,我在CLion中使用CMake,而不是命令窗口,因此我不确定如何使用此命令(假设它可以工作).其次,上面指定的路径似乎在我的计算机上不存在-/usr中没有X11R6文件夹.因此,如何正确链接所需的X11库,以便可以开始使用CImg?

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?

(此外,我不知道这是否相关,但我没有安装Magick ++.)

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

推荐答案

CImg 程序的构建方式令人遗憾的是,该文档中并没有很清楚.这是我的看法:

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

步骤1-您需要在屏幕上显示图像吗?

不,请不要笑,您可能正在无头Web服务器上处理图像.

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

  • 如果您不需要需要显示图像,则必须先执行#define cimg_display 0 ,然后再添加"CImg.h"
  • 如果确实需要显示图像,并且您使用的是Windows,则需要链接gdi32
  • 如果确实需要显示图像,并且您使用的是macOS,OSX或Unix/Linux,则需要设置X11的标头包含路径并与之链接
  • 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

第2步-您需要读/写PNG格式的图像吗?

  • 如果您不需要需要此功能,或者您很乐意让 CImg 使用您已安装的 ImageMagick 读取/写入PNG图像,什么也不做.

  • 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.

如果要使用 CImg 的内置功能来读取/写入PNG图像,则需要#define cimg_use_png 1 之前,您必须包含,并且您需要设置 libpng 的标头包含路径,并同时链接libpng libz.

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.

第3步-您需要读/写JPEG格式的图像吗?

  • 如果您不需要需要此功能,或者您很乐意让 CImg 使用您已安装的 ImageMagick 读/写JPEG图像,什么也不做.

  • 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.

如果您想使用 CImg 的内置功能来读取/写入JPEG图像,则需要#define cimg_use_jpeg 1 之前,其中包括,并且您需要为 libjpeg 设置标头包含路径,并与libjpeg链接.

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.

第4步-其他内容

另一件事是,您可能需要链接数学和POSIX线程库(使用-lm -lpthread作为链接器开关),但是我不确定如何在所有平台上进行检查,所以我将其留给您-添加它可能不会造成伤害.

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;
}

您在CLion内的 CMake CmakeLists.txt如下所示:

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!

这篇关于使用CLion和CMake的CImg X11链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆