linux CMake链接静态库和Glib错误

wfypjpf4  于 2023-01-01  发布在  Linux
关注(0)|答案(1)|浏览(199)

我对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
)

在这两种情况下我都保留了未定义的引用。感谢大家的帮助,我一直在论坛上寻找提示或解释。
编辑:

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
nfzehxib

nfzehxib1#

我发现了这个问题,它与需要包含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库合并到他们的构建中的人。

相关问题