【问题标题】:Multiples CMake in a project项目中的多个 CMake
【发布时间】:2016-04-24 15:18:10
【问题描述】:

我正在开发一个使用我之前开发的库的 C++ 应用程序。我不想将二进制文件链接到我的新项目,因为使用另一个平台的人需要重建。所以,我想将另一个项目中的 cmake 文件附加到这个新项目中,然后当我构建时,也构建库。有人知道我是怎么做到的吗?非常感谢。
字符串库:https://github.com/TigreFramework/String
我的新 CMake 文件:

cmake_minimum_required(VERSION 3.4)
project(DataBase)

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

set(SOURCE_FILES main.cpp DataBase.cpp DataBase.h String.h libString.a)
add_executable(DataBase ${SOURCE_FILES})

编辑 01:

现在我可以用这个 CMake 编译了:

cmake_minimum_required(VERSION 3.4)
project(String)

include_directories(./include/cryptopp563/)
#link_directories(./include/cryptopp563/)
add_subdirectory(./include/cryptopp563/)

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

find_library(cryptopp include/cryptopp563/)


set(SOURCE_FILES main.cpp String.cpp String.h RsaString.cpp RsaString.h)

add_library(RsaString STATIC RsaString.cpp)
add_library(String STATIC String.cpp)

#add_executable(Teste ${SOURCE_FILES})

但是,如果我使用 de add_executable 我会收到此错误:

[  1%] Built target String
[  2%] Built target RsaString
[  2%] Linking CXX executable Teste
[ 97%] Built target cryptopp
Undefined symbols for architecture x86_64:
"CryptoPP::RandomPool::IncorporateEntropy(unsigned char const*, unsigned long)", referenced from:
vtable for CryptoPP::AutoSeededRandomPool in String.cpp.o
"CryptoPP::RandomPool::GenerateIntoBufferedTransformation(CryptoPP::BufferedTransformation&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long)", referenced from:
vtable for CryptoPP::AutoSeededRandomPool in String.cpp.o
"CryptoPP::RandomPool::RandomPool()", referenced from:
CryptoPP::AutoSeededRandomPool::AutoSeededRandomPool(bool, unsigned int) in String.cpp.o
.............. MORE LINES HIDEM HERE .................
"non-virtual thunk to CryptoPP::BufferedTransformation::GetWaitObjects(CryptoPP::WaitObjectContainer&, CryptoPP::CallStack const&)", referenced from:
vtable for CryptoPP::SourceTemplate<CryptoPP::FileStore> in RsaString.cpp.o
vtable for CryptoPP::FileSource in RsaString.cpp.o
vtable for CryptoPP::AutoSignaling<CryptoPP::InputRejecting<CryptoPP::BufferedTransformation> > in RsaString.cpp.o
vtable for CryptoPP::InputRejecting<CryptoPP::BufferedTransformation> in RsaString.cpp.o
vtable for CryptoPP::InputRejecting<CryptoPP::Filter> in RsaString.cpp.o
vtable for CryptoPP::Sink in RsaString.cpp.o
vtable for CryptoPP::PK_DecryptorFilter in RsaString.cpp.o
...
"non-virtual thunk to CryptoPP::BufferedTransformation::GetMaxWaitObjectCount() const", referenced from:
vtable for CryptoPP::SourceTemplate<CryptoPP::FileStore> in RsaString.cpp.o
vtable for CryptoPP::FileSource in RsaString.cpp.o
vtable for CryptoPP::AutoSignaling<CryptoPP::InputRejecting<CryptoPP::BufferedTransformation> > in RsaString.cpp.o
vtable for CryptoPP::InputRejecting<CryptoPP::BufferedTransformation> in RsaString.cpp.o
vtable for CryptoPP::InputRejecting<CryptoPP::Filter> in RsaString.cpp.o
vtable for CryptoPP::Sink in RsaString.cpp.o
vtable for CryptoPP::PK_DecryptorFilter in RsaString.cpp.o
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/Teste.dir/build.make:146: recipe for target 'Teste' failed
gmake[2]: *** [Teste] Error 1
CMakeFiles/Makefile2:141: recipe for target 'CMakeFiles/Teste.dir/all' failed
gmake[1]: *** [CMakeFiles/Teste.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
gmake: *** [all] Error 2

【问题讨论】:

    标签: c++ cmake


    【解决方案1】:

    你可以使用add_subdirectoryhttps://cmake.org/cmake/help/v3.0/command/add_subdirectory.html

    或简单的包含 https://cmake.org/cmake/help/v3.0/command/include.html

    还有一个叫做外部项目的cmake特性, 它允许您使用任何构建系统构建项目

    https://cmake.org/cmake/help/v3.0/module/ExternalProject.html

    【讨论】:

    • 这应该是一条评论。 Link-only answers are discouraged
    • @PedroSoares 我不明白,如果你在你的Teste 中使用库,你必须使用target_link_libraries(Teste library),为什么你认为它可以在不链接的情况下使用某些功能?
    • 当我放 target_link_libraries 时,我从我的 IDE 收到错误错误:无法为目标“cryptopp”指定链接库,该目标不是由该项目构建的。
    • 你链接Teste,为什么报错cryptopp?以任何方式将IDE从讨论中删除,从命令行使用cmake,直到错误得到解决
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多