gpt4 book ai didi

创建可执行文件时的CMake undefined reference

转载 作者:行者123 更新时间:2023-12-01 08:52:46 24 4
gpt4 key购买 nike

我是 CMake 新手,我正在尝试编译我的项目。该项目创建了一些静态库和一些可执行文件。

以下是我拥有的文件结构的示例。

项目

  • SRC

    1. 子项目_1
      此文件夹的 .cpp(所有源文件)和 CMakeLists.txt 1(创建静态库)
    2. 子项目_2
      此文件夹的 .cpp(所有源文件)和 CMakeLists.txt 2(创建静态库)
    3. 子项目_3
      .cpp(所有源文件)和 CMakeLists.txt 3 用于此文件夹(创建可执行文件)
  • 包括

    1. 子项目_1
      .h(所有头文件)
    2. 子项目_2
      .h(所有头文件)
    3. 子项目_3
      .h(所有头文件)
  • 构建/linux

    1. CMakeLists.txt(主 CMakelist 文件)

主 CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
SET(CMAKE_CXX_COMPILER "g++")
Project(ort)
SET (CMAKE_CXX_FLAGS " -g -Wall -pThread")

#set the source and header directories location
set(ORT_HEADER_DIRECTORY "../../include") #include folder structure explained above
set(ORT_SOURCE_DIRECTORY "../../src")
set(ORT_BINARY_DIRECTORY "../../lib") # lib folder to contain all the libraries

set (CMAKE_CURRENT_BINARY_DIR ".")

#Include the library packages
include_directories("/usr/include/wx-2.8")
include_directories("/usr/local/cuda/include") and so on


#set the names of all the projects (for creating the libraries)
SET(PROJECT_NAMES "log" "data" "cc")

foreach(PROJECT_NAME ${PROJECT_NAMES})
# Create the cmake related files in the out folder so that the libraries can be
# copied to the lib folder
add_subdirectory( "{ORT_SOURCE_DIRECTORY}/${PROJECT_NAME}" "${CMAKE_CURRENT_BINARY_DIR}/out/${PROJECT_NAME}"

endforeach(PROJECT_NAME ${PROJECT_NAMES})

#set the names of all the projects (for creating the libraries)
SET(EXECUATALE_PROJECTS "metadata" )

foreach(EXECUATALE_PROJECT ${EXECUATALE_PROJECTS})
# Create the cmake related files in the out folder so that the libraries can be
# copied to the lib folder
add_subdirectory( "{ORT_SOURCE_DIRECTORY}/${EXECUATALE_PROJECT}" "${CMAKE_CURRENT_BINARY_DIR}/out/${EXECUATALE_PROJECT}"

endforeach(EXECUATALE_PROJECT ${EXECUATALE_PROJECTS})

日志目录的 CMakeLists.txt 文件(与我用于 cc 和数据项目的逻辑相同)

include_directories(${ORT_HEADER_DIRECTORY})
SET(LOG_SOURCE a.cpp b.cpp c.cpp)
ADD_LIBRARY(log_d ${LOG_SOURCE})
target_link)libraries(log_d cc_d data_d)

元数据 CMakeLists.txt 文件(创建可执行项目)

FIND_PACKAGE(wxWidgets)
IF(wxWidgets_FOUND)
INCLUDE(${wxWidgets_USE_FILE})
ENDIF(wxWidgets_FOUND)

Include_Directories(${wxWidgets_INCLUDE_DIRS})
include_directories(${ORT_HEADER_DIRECTORY})
include_directories("/usr/ort/lib/unixODBC/include")

SET(META_SOURCE meta.cpp data.cpp)

ADD_EXECUTABLE(meta_d ${META_SOURCE })
TARGET_LINK_LIBRARIES(meta_d log_d data_d)

当我只是制作项目时,没有创建可执行文件,正在生成静态文件。但是,当我制作整个项目时(即包含 subProject_3 目录),我得到了对 a::String 的 undefined reference ,它是 a.cpp 中的一个函数。

注意:所有 3 个项目都是相互依赖的。例如,在 a.cpp 中,我有 #include "b.h",在 b.cpp 中我有 #include "a2.h"

所以,我有几个问题:

a) 如何解决 undefined reference 问题?为项目 1 和 2 生成库后,如何将它们链接到可执行文件?

b) 在创建静态库时我应该提供或添加任何依赖项吗?这是创建静态库的正确方法(因为项目是相互依赖的)?即项目 1 中的 target_link_libraries(project1 project2 ...) 和项目 2 中的 target_link_libraries(project2 project1 ...)

c) 每个项目都需要使用自己的编译设置进行编译。您能否告诉我如何为每个单独的项目指定相同的内容?

错误详情:

喜欢 CXX 可执行元数据_d../log/liblog_d.a:文件无法识别:文件被截断collect2: ld 返回 1 个退出状态

我也得到 undefined reference 错误/home...../metadata/data.cpp 172:对 xmlSerahNs 的 undefined reference 等等。collect2: ld 返回 1 个退出状态

感谢您的帮助。

最佳答案

How do I resolve the undefined reference issue ? Since, I have generating the libraries for project 1 and 2, how do I link these to the executable.

只需使用 target_link_libraries再次,这次是在 subProject3 的 CMakeLists.txt 中:

target_link_libraries(subProject3 subProject2 subProject1)

Is this the right way of creating the static libraries (as the projects are inter dependent)

是的。来自 target_link_libraries 的文档:

The library dependency graph is normally acyclic (a DAG), but in the case of mutually-dependent STATIC libraries CMake allows the graph to contain cycles (strongly connected components). When another target links to one of the libraries CMake repeats the entire connected component. For example, the code

add_library(A STATIC a.c)
add_library(B STATIC b.c)
target_link_libraries(A B)
target_link_libraries(B A)
add_executable(main main.c)
target_link_libraries(main A)

links 'main' to 'A B A B'. (While one repetition is usually sufficient, pathological object file and symbol arrangements can require more. One may handle such cases by manually repeating the component in the last target_link_libraries call. However, if two archives are really so interdependent they should probably be combined into a single archive.)


Every project needs to be compiled with there own compilation setting. Can you please let me know how can I specify the same for every individual project?

在顶层 CMakeLists.txt 中,在添加子目录之前,您可以在其中设置所有需要的标志。 (例如,参见 add_definitionsProperties on Targets)它们将被所有子项目采用,除非针对子项目进行特殊更改。

关于创建可执行文件时的CMake undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13182805/

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