2016-04-24 103 views
-1

我正在開發一個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 

回答

0

您可以使用add_subdirectory https://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

+1

此建項目應該是一個評論。 [只有鏈接的答案不鼓勵](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) –

+0

add_subdirectory不工作就像我想。 –

+0

@PedroSoares我不明白,如果你在'Teste'中使用庫,你必須使用'target_link_libraries(Teste庫)',爲什麼你認爲它可以使用一些沒有鏈接的功能呢? – fghj