gpt4 book ai didi

c++ - CMake 链接静态库和 Glib 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:32 25 4
gpt4 key购买 nike

我对 Cmake 的工作原理了解甚少。我有一个项目,其中包含一个依赖于 glib 的静态库。我遗漏了一些导致大量 undefined references to glib features 的东西,如下所示。我在 Linux Ubuntu 中工作并且有 Cmake 3.5

../Gobbledegook/src/libggk.a(libggk_a-Gobbledegook.o): In function `ggkWait':
/home/ubuntu/visionpro-device-xray/src/user_device_interface/Gobbledegook/src/Gobbledegook.cpp:384: undefined reference to `g_set_print_handler'
/home/ubuntu/visionpro-device-xray/src/user_device_interface/Gobbledegook/src/Gobbledegook.cpp:385: undefined reference to `g_set_printerr_handler'
/home/ubuntu/visionpro-device-xray/src/user_device_interface/Gobbledegook/src/Gobbledegook.cpp:386: undefined reference to `g_log_set_default_handler'

这是我的 Cmake 文件,我在其中尝试自己构建库 - 而没有链接静态库。我尝试使用/包含的库是 https://github.com/nettlep/gobbledegook .

cmake_minimum_required(VERSION 3.2.2)
project(TestBLE)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -I/usr/include/glib-2.0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/lib/aarch64-linux-gnu/glib-2.0/include")

find_package(PkgConfig REQUIRED)
pkg_search_module(GLIB REQUIRED glib-2.0)
include_directories(${GLIB_INCLUDE_DIRS})
link_directories(${GLIB_LIBRARY_DIRS})

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

# Gets all the header/source files in current directory
file(GLOB SOURCES "*.cpp")
file(GLOB HEADERS "*.h")

file(GLOB GGK_SOURCES "Gobbledegook/src/*.cpp")
file(GLOB GGK_HEADERS "Gobbledegook/src/*.h")

add_executable(
${PROJECT_NAME}
${SOURCES}
${HEADERS}
${CMAKE_SOURCE_DIR}/Gobbledegook/include/Gobbledegook.h
${GGK_SOURCES}
${GGK_HEADERS}
)

target_link_libraries(
${PROJECT_NAME}
${GLIB_LIBRARIES}
Threads::Threads
)

这是我的 CMake 文件,它试图链接静态库 libggk.a,该静态库是使用 Gobbledegook 库中的 ./configure && make 构建的。

cmake_minimum_required(VERSION 3.2.2)
project(TestBLE)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -I/usr/include/glib-2.0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/lib/aarch64-linux-gnu/glib-2.0/include")

find_package(PkgConfig REQUIRED)
pkg_search_module(GLIB REQUIRED glib-2.0)
include_directories(${GLIB_INCLUDE_DIRS})
link_directories(${GLIB_LIBRARY_DIRS})

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

# Gets all the header/source files in current directory
file(GLOB SOURCES "*.cpp")
file(GLOB HEADERS "*.h")

add_executable(
${PROJECT_NAME}
${SOURCES}
${HEADERS}
${CMAKE_SOURCE_DIR}/Gobbledegook/include/Gobbledegook.h
)

target_link_libraries(
${PROJECT_NAME}
${GLIB_LIBRARIES}
Threads::Threads
${CMAKE_SOURCE_DIR}/Gobbledegook/src/libggk.a
)

无论哪种情况,我都保留 undefined reference 。感谢大家的帮助,我一直在搜索论坛以寻求提示或解释。

编辑:

here is my directory setup
project
|---------CMakeLists.txt
|---------build
|---------bluetest.cpp
|---------Gobbledegook
| |-----------include
| | |------Gobbldegook.h
| |-----------src
| | |------${all_the_source/header_files}
| | |------libggk.a

编辑 2:修改 Cmake 以显示打印件

cmake_minimum_required(VERSION 3.2.2)
project(TestBLE)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB REQUIRED glib-2.0)
include_directories(Gobbledegook/include)
include_directories(${GLIB_INCLUDE_DIRS})
link_directories(${GLIB_LIBRARIES_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GLIB_CFLAGS}")

message(WARNING "GLIB_LIBRARIES: ${GLIB_LIBRARIES}")
message(WARNING "GLIB_LIBRARY_DIRS: ${GLIB_LIBRARY_DIRS}")
message(WARNING "GLIB_LDFLAGS: ${GLIB_LDFLAGS}")
message(WARNING "GLIB_LDFLAGS_OTHER: ${GLIB_LDFLAGS_OTHER}")
message(WARNING "GLIB_INCLUDE_DIRS: ${GLIB_INCLUDE_DIRS}")
message(WARNING "GLIB_CFLAGS: ${GLIB_CFLAGS}")
message(WARNING "GLIB_CFLAGS_OTHER: ${GLIB_CFLAGS_OTHER}")

pkg_check_modules(DBUS REQUIRED dbus-1)
include_directories(${DBUS_INCLUDE_DIRS})
link_directories(${DBUS_LIBRARIES_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DBUS_CFLAGS}")

message(WARNING "DBUS_LIBRARIES: ${DBUS_LIBRARIES}")
message(WARNING "DBUS_LIBRARY_DIRS: ${DBUS_LIBRARY_DIRS}")
message(WARNING "DBUS_LDFLAGS: ${DBUS_LDFLAGS}")
message(WARNING "DBUS_LDFLAGS_OTHER: ${DBUS_LDFLAGS_OTHER}")
message(WARNING "DBUS_INCLUDE_DIRS: ${DBUS_INCLUDE_DIRS}")
message(WARNING "DBUS_CFLAGS: ${DBUS_CFLAGS}")
message(WARNING "DBUS_CFLAGS_OTHER: ${DBUS_CFLAGS_OTHER}")

pkg_check_modules(DBUS_GLIB REQUIRED dbus-glib-1)
include_directories(${DBUS_GLIB_INCLUDE_DIRS})
link_directories(${DBUS_GLIB_LIBRARIES_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DBUS_GLIB_CFLAGS}")

message(WARNING "DBUS_GLIB_LIBRARIES: ${DBUS_GLIB_LIBRARIES}")
message(WARNING "DBUS_GLIB_LIBRARY_DIRS: ${DBUS_GLIB_LIBRARY_DIRS}")
message(WARNING "DBUS_GLIB_LDFLAGS: ${DBUS_GLIB_LDFLAGS}")
message(WARNING "DBUS_GLIB_LDFLAGS_OTHER: ${DBUS_GLIB_LDFLAGS_OTHER}")
message(WARNING "DBUS_GLIB_INCLUDE_DIRS: ${DBUS_GLIB_INCLUDE_DIRS}")
message(WARNING "DBUS_GLIB_CFLAGS: ${DBUS_GLIB_CFLAGS}")
message(WARNING "DBUS_GLIB_CFLAGS_OTHER: ${DBUS_GLIB_CFLAGS_OTHER}")

message(WARNING "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")

set(THREADS_PREFER_PTHREAD_FLAG ON)
FIND_PACKAGE(Threads REQUIRED)

# Gets all the header/source files in current directory
file(GLOB SOURCES "*.cpp")
file(GLOB HEADERS "*.h")

file(GLOB GGK_SOURCES "Gobbledegook/src/*.cpp")
file(GLOB GGK_HEADERS "Gobbledegook/src/*.h")

add_executable(
${PROJECT_NAME}
${SOURCES}
${HEADERS}
#${CMAKE_SOURCE_DIR}/Gobbledegook/include/Gobbledegook.h
${GGK_SOURCES}
${GGK_HEADERS}
)

target_include_directories(
${PROJECT_NAME}
PUBLIC ${GLIB_INCLUDE_DIRS}
PUBLIC ${DBUS_INCLUDE_DIRS}
PUBLIC ${DBUS_GLIB_INCLUDE_DIRS}
)

target_link_libraries(
${PROJECT_NAME}
PUBLIC ${GLIB_LIBRARIES}
PUBLIC ${DBUS_LIBRARIES}
PUBLIC ${DBUS_GLIB_LIBRARIES}
Threads::Threads
)

修改后的 CMake 文件的输出

ubuntu@tegra-ubuntu:~/visionpro-device-xray/src/user_device_interface/build$ sudo cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1")
-- Checking for module 'glib-2.0'
-- Found glib-2.0, version 2.48.2
CMake Warning at CMakeLists.txt:15 (message):
GLIB_LIBRARIES: glib-2.0

CMake Warning at CMakeLists.txt:16 (message):
GLIB_LIBRARY_DIRS:


CMake Warning at CMakeLists.txt:17 (message):
GLIB_LDFLAGS: -lglib-2.0

CMake Warning at CMakeLists.txt:18 (message):
GLIB_LDFLAGS_OTHER:

CMake Warning at CMakeLists.txt:19 (message):
GLIB_INCLUDE_DIRS:
/usr/include/glib-2.0;/usr/lib/aarch64-linux-gnu/glib-2.0/include

CMake Warning at CMakeLists.txt:20 (message):
GLIB_CFLAGS:
-I/usr/include/glib-2.0;-I/usr/lib/aarch64-linux-gnu/glib-2.0/include

CMake Warning at CMakeLists.txt:21 (message):
GLIB_CFLAGS_OTHER:

-- Checking for module 'dbus-1'
-- Found dbus-1, version 1.10.6
CMake Warning at CMakeLists.txt:28 (message):
DBUS_LIBRARIES: dbus-1

CMake Warning at CMakeLists.txt:29 (message):
DBUS_LIBRARY_DIRS:

CMake Warning at CMakeLists.txt:30 (message):
DBUS_LDFLAGS: -ldbus-1

CMake Warning at CMakeLists.txt:31 (message):
DBUS_LDFLAGS_OTHER:

CMake Warning at CMakeLists.txt:32 (message):
DBUS_INCLUDE_DIRS:
/usr/include/dbus-1.0;/usr/lib/aarch64-linux-gnu/dbus-1.0/include

CMake Warning at CMakeLists.txt:33 (message):
DBUS_CFLAGS:
-I/usr/include/dbus-1.0;-I/usr/lib/aarch64-linux-gnu/dbus-1.0/include

CMake Warning at CMakeLists.txt:34 (message):
DBUS_CFLAGS_OTHER:

-- Checking for module 'dbus-glib-1'
-- Found dbus-glib-1, version 0.106
CMake Warning at CMakeLists.txt:41 (message):
DBUS_GLIB_LIBRARIES: dbus-glib-1;dbus-1;gobject-2.0;glib-2.0

CMake Warning at CMakeLists.txt:42 (message):
DBUS_GLIB_LIBRARY_DIRS:

CMake Warning at CMakeLists.txt:43 (message):
DBUS_GLIB_LDFLAGS: -ldbus-glib-1;-ldbus-1;-lgobject-2.0;-lglib-2.0

CMake Warning at CMakeLists.txt:44 (message):
DBUS_GLIB_LDFLAGS_OTHER:

CMake Warning at CMakeLists.txt:45 (message):
DBUS_GLIB_INCLUDE_DIRS:
/usr/include/dbus-1.0;/usr/lib/aarch64-linux-gnu/dbus-1.0/include;/usr/include/glib-2.0;/usr/lib/aarch64-linux-gnu/glib-2.0/include

CMake Warning at CMakeLists.txt:46 (message):
DBUS_GLIB_CFLAGS:
-I/usr/include/dbus-1.0;-I/usr/lib/aarch64-linux-gnu/dbus-1.0/include;-I/usr/include/glib-2.0;-I/usr/lib/aarch64-linux-gnu/glib-2.0/include

CMake Warning at CMakeLists.txt:47 (message):
DBUS_GLIB_CFLAGS_OTHER:

CMake Warning at CMakeLists.txt:49 (message):
CMAKE_CXX_FLAGS: -std=c++11
-I/usr/include/glib-2.0;-I/usr/lib/aarch64-linux-gnu/glib-2.0/include
-I/usr/include/dbus-1.0;-I/usr/lib/aarch64-linux-gnu/dbus-1.0/include
-I/usr/include/dbus-1.0;-I/usr/lib/aarch64-linux-gnu/dbus-1.0/include;-I/usr/include/glib-2.0;-I/usr/lib/aarch64-linux-gnu/glib-2.0/include

-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/visionpro-device-xray/src/user_device_interface/build

最佳答案

我发现了问题,它与需要包含 gtk+-2.0 有关。我不知道为什么在我链接的库中使用 gtk+-2.0,因为它不是图形程序。 CmakeLists.txt 应如下所示。

cmake_minimum_required(VERSION 3.2.2)
project(TestBLE)

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") #All warning
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra") #Extra warning flags

#####################################################################
# Including Packages
#####################################################################
find_package(PkgConfig REQUIRED)

pkg_check_modules(GLIB REQUIRED glib-2.0)
include_directories(${GLIB_INCLUDE_DIRS})

pkg_check_modules(GTK2 REQUIRED gtk+-2.0)
include_directories(${GTK2_INCLUDE_DIRS})

set(THREADS_PREFER_PTHREAD_FLAG ON)
FIND_PACKAGE(Threads REQUIRED)
#####################################################################
# Files to add to the executable
#####################################################################
file(GLOB SOURCES "*.cpp")
file(GLOB HEADERS "*.h")

file(GLOB GGK_SOURCES "Gobbledegook/src/*.cpp")
file(GLOB GGK_HEADERS "Gobbledegook/src/*.h")

add_executable(
${PROJECT_NAME}
${SOURCES}
${HEADERS}
#${CMAKE_SOURCE_DIR}/Gobbledegook/include/Gobbledegook.h
${GGK_SOURCES}
${GGK_HEADERS}
)

target_include_directories(
${PROJECT_NAME}
PUBLIC ${GLIB_INCLUDE_DIRS}
)

target_link_libraries(
${PROJECT_NAME}
PUBLIC ${GLIB_LIBRARIES}
PUBLIC ${GTK2_LIBRARIES}
Threads::Threads
)

希望这对尝试将 Gobbledegook 库合并到他们的构建中的其他人有所帮助。

关于c++ - CMake 链接静态库和 Glib 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55067881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com