gpt4 book ai didi

c++ - CMAKE LINKING ERROR 与用户定义的共享库

转载 作者:行者123 更新时间:2023-11-30 02:46:10 26 4
gpt4 key购买 nike

我是 cmake 世界的新手。当我在 C++ 中试验一些基本的 cmake 配置时,出现了这个问题。准确地说,以下是我的目录结构:

/源----

 |-> CMakeLists.txt
|-> main.cpp
|-> lib----
|-> libfsystem.so
|->filesystem----
|->CMakeLists.txt
|->listfiles.cpp
|->include-----
|->fsystem.h

现在,/src/filesystem/CMakeLists.txt 文件是这样的

cmake_minimum_required(VERSION 2.8)
project(fsystem)
set(CMAKE_BUILD_TYPE Release)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/build)
find_package(Boost REQUIRED system filesystem)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(fsystem SHARED listfiles.cpp)

而/src/CMakeLists.txt文件是这样的

cmake_minimum_required(VERSION 2.8)

project(vessel_detect)

add_subdirectory(filesystem)

add_executable(main main.cpp)

set(CMAKE_LIBRARY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib)
find_library(libpath fsystem)
MESSAGE(${libpath})
target_link_libraries(main ${libpath})
  1. 库 libfsystem.so 已成功创建。
  2. 库 libfsystem.so 也被/src/CMakeLists.txt 成功找到

然而,当 main.cpp 的链接完成后,它会给我几个 undefined reference 错误,这些错误不应该发生,因为所有内容都已经定义好了。为了更完整,以下是 main.cpp 文件的内容

main.cpp

#include<iostream>
#include"filesystem/include/fsystem.h"
using namespace std;
int main(void)
{
string path ("~");
vector<string>* output;
output = listfiles(path);
return 0;
}

listfiles.cpp的内容是

listfiles.cpp

#include"fsystem.h"
using namespace boost::filesystem;
vector<string>* listfiles(int argc, char* argv[])
{
if (argc<2)
{
std::cout<<"No file name provided"<<std::endl;
}
else
{
path p(argv[1]);
vector<string>* output;
if (exists(p))
{
if(is_directory(p))
{
std::cout<<"You specified a directory"<<std::endl;
std::cout<<"Its contents are as follows :-"<<std::endl;
typedef std::vector<path> vec;
vec v;
copy(directory_iterator(p),directory_iterator(),back_inserter(v));
sort(v.begin(),v.end());
for(vec::const_iterator it(v.begin());it!=v.end();++it)
output->push_back(it->filename().string());
// std::cout<<it->filename()<<std::endl;
}
else if (is_regular_file(p))
{
std::cout<<argv[1]<<" "<<file_size(p)<<std::endl;
}
else
{
std::cout<<"The file is neither a directory nor a regular file"<<std::endl;
}
}
else
{
std::cout<<"The speicified path does not exist"<<std::endl;
}
}
}

最后,fsystem.h 的内容是:

fsystem.h

#ifndef _fsystem_h
#define _fsystem_h
#include<iostream>
#include<string>
#include<vector>
#include"boost/filesystem.hpp"
using namespace std;
vector<string>* listfiles(string);
#endif

有人可以为我在链接 main.cpp 期间遇到的 undefined reference 错误提供原因吗?如果您能为我提供此问题的解决方案,我将不胜感激。

谢谢

最佳答案

您没有链接提升。您需要将 target_link_libraries(fsystem ${Boost_LIBRARIES}) 添加到/src/filesystem/CMakeLists.txt 的末尾,并在 find_package 和添加_库。

cmake_minimum_required(VERSION 2.8)
project(fsystem)
set(CMAKE_BUILD_TYPE Release)
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/build)
find_package(Boost REQUIRED system filesystem)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${Boost_INCLUDE_DIRS})
add_library(fsystem SHARED listfiles.cpp)
target_link_libraries(fsystem ${Boost_LIBRARIES})

关于c++ - CMAKE LINKING ERROR 与用户定义的共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23811092/

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