我正在开发一个C++应用程序,使用我以前开发的库。我不希望将二进制文件链接到我的新项目,因为谁使用另一个plataform需要重建。所以,我想把这个另一个项目的cmake文件附加到这个新的项目中,当我编译的时候,也建立这个库。有人知道我是如何做到这一点的?非常感谢。
字符串库:https://github.com/TigreFramework/String
我的新CMake的文件: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})
但是,如果我用德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
此建项目应该是一个评论。 [只有链接的答案不鼓励](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) –
add_subdirectory不工作就像我想。 –
@PedroSoares我不明白,如果你在'Teste'中使用库,你必须使用'target_link_libraries(Teste库)',为什么你认为它可以使用一些没有链接的功能呢? – fghj