gpt4 book ai didi

cmake 未定义的函数引用

转载 作者:行者123 更新时间:2023-12-03 01:52:55 27 4
gpt4 key购买 nike

下面的项目结构是一个简化的示例。我尝试将其归结为最少量的文件来重现我的问题。

.
├── CMakeLists.txt
├── subdir1
│   ├── CMakeLists.txt
│   └── subsubdir1
│   ├── CMakeLists.txt
│   ├── Example.cpp
│   └── Example.h
└── subdir2
├── CMakeLists.txt
├── main.cpp
└── subsubdir1
├── CMakeLists.txt
├── ExampleCreator.cpp
└── ExampleCreator.h

./CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(test)

macro(add_sources)
file (RELATIVE_PATH _relPath "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}")
foreach (_src ${ARGN})
if (_relPath)
list (APPEND SRCS "${_relPath}/${_src}")
else()
list (APPEND SRCS "${_src}")
endif()
endforeach()
if (_relPath)
# propagate SRCS to parent directory
set (SRCS ${SRCS} PARENT_SCOPE)
endif()
endmacro()

add_subdirectory(subdir1)
add_subdirectory(subdir2)

add_executable(test ${SRCS})

subdir1/CMakeLists.txt

add_subdirectory(subsubdir1)

subdir1/subsubdir1/CMakeLists.txt

add_sources(Example.cpp)

subdir1/subsubdir1/Example.h

#ifndef EXAMPLE_H
#define EXAMPLE_H

class Example
{
public:
Example();
virtual ~Example();
};

#endif

subdir1/subsubdir1/Example.cpp

#include <stdio.h>
#include "Example.h"

Example::Example()
{
printf("Inside Example constructor\n");
}

Example::~Example()
{
}

subdir2/CMakeLists.txt

add_subdirectory(subsubdir1)
add_sources(main.cpp)

subdir2/main.cpp

#include "subsubdir1/ExampleCreator.h"

int main(int argc, char** argv)
{
ExampleCreator creator;
return 0;
}

subdir2/subsubdir1/CMakeLists.txt

add_sources(ExampleCreator.cpp)

subdir2/subsubdir1/ExampleCreator.h

#ifndef EXAMPLE_CREATOR_H
#define EXAMPLE_CREATOR_H

class ExampleCreator
{
public:
ExampleCreator();
virtual ~ExampleCreator();
};

#endif

subdir2/subsubdir1/ExampleCreator.cpp

#include "ExampleCreator.h"
#include "../../subdir1/subsubdir1/Example.h"

ExampleCreator::ExampleCreator()
{
Example* ex1 = new Example();
}

ExampleCreator::~ExampleCreator()
{
}

我希望这只是对 CMake 如何处理依赖关系缺乏了解。编译时没有错误,但在链接期间失败。下面的 make 输出显示 Example.cpp 甚至没有编译,我不明白为什么。

user>:~/src/test/build$ make
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/subdir2/subsubdir1/ExampleCreator.cpp.o
[100%] Building CXX object CMakeFiles/test.dir/subdir2/main.cpp.o
Linking CXX executable test
CMakeFiles/test.dir/subdir2/subsubdir1/ExampleCreator.cpp.o: In function `ExampleCreator::ExampleCreator()':
ExampleCreator.cpp:(.text+0x2b): undefined reference to `Example::Example()'
collect2: ld returned 1 exit status
make[2]: *** [test] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

据我所知,所有源代码都附加到根 CMakeLists.txt 文件中的 SRCS 变量中。那么,为什么 Example.cpp 没有被编译呢?或链接?

最佳答案

目录subdir1不会生成二进制文件。添加此:

project(Example)
add_library(Example Example.cpp)

而不是您的add_sources。之后,您需要告诉使用它的项目来链接它:

TARGET_LINK_LIBRARIES(subdir2 Example)

如果您的名字不同,请自行记录这些命令的功能。

关于cmake 未定义的函数引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13057107/

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