【问题标题】:how can set address of libraries in Cmakelist如何在 Cmakelist 中设置库的地址
【发布时间】:2020-07-27 15:36:07
【问题描述】:

我是 CMake 及其使用方法的初学者。我的 IDE 是 Clion,我尝试为 c++ 安装 gtk3 并按照所有说明进行操作,但他们建议用于测试的简单示例代码对我不起作用。您可以找到说明here。这是代码:

#include <gtkmm.h>

int main(int argc, char** argv)
{
    auto app = Gtk::Application::create(argc, argv);

    Gtk::Window window;
    window.set_default_size(600,400);

    return app->run(window);
}

错误是fatal error: gtkmm.h: No such file or directory,但是当我将#include &lt;gtkmm.h&gt;更改为#include &lt;gtkmm-3.0/gtkmm.h&gt;时,这里不再出现错误,但在fatal error: glibmm.h: No such file or directory等其他子标题中出现错误。它只需要以某种方式告诉编译器查看子目录,但我不知道该怎么做。我认为它可以在 CmakeLists.txt 文件中修复,但我不知道该怎么做。这是我的 CmakeLists.txt 文件:

cmake_minimum_required(VERSION 3.16)
project(gtk)

set(CMAKE_CXX_STANDARD 14)

add_executable(gtk main.cpp)

如果有人知道它对我有帮助,请告诉我。

【问题讨论】:

  • 欢迎来到 Stack Overflow!在这里,我们鼓励在提问之前搜索。 “我认为它可以在 CmakeLists.txt 文件中修复,但我不知道该怎么做。” - 您是否尝试过搜索该主题? Stack Overflow 上很多问题都已经包含CMakeLists.txt,你试过了吗?例如。 that one.
  • 要使用CMake,需要编写一个CMakeLists.txt文件。

标签: c++ c cmake gtk3 clion


【解决方案1】:

您没有正确链接到 gtk 库。

您需要在您的 CMakeLists.txt 中使用find_package(),如下所示:

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK REQUIRED gtk+-3.0)

include_directories(${GTK_INCLUDE_DIRS})
link_directories(${GTK_LIBRARY_DIRS})
add_definitions(${GTK_CFLAGS_OTHER})

add_executable(gtkdemo main.cpp)
target_link_libraries(gtkdemo ${GTK_LIBRARIES})

更多信息,您可以参考以下内容:

'undefined reference to' when using GTK with CMake

How do I link gtk library more easily with cmake in windows?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-02
    • 2010-12-05
    • 2023-03-03
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多