2016-07-07 79 views
0

這是我的項目結構:創建cmake的安裝宏庫

main 
| 
--src 
    | 
    --feed.h 
    --feed.cc 
    --other files 
    --CMakeLists2.txt 
--test 
    | 
    --test.cpp 
    --CMakeLists3.txt 
CMakeLists1.txt 

CMakeLists1.txt

cmake_minimum_required (VERSION 2.8.11) 
project (Feedparser) 

set(CMAKE_MODULE_PATH cmake) 

find_package(PTHREAD REQUIRED) 
find_package(CURL REQUIRED) 

include(CheckCXXCompilerFlag) 
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX11) 
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X) 
if(COMPILER_SUPPORTS_CXX11) 
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 ") 
elseif(COMPILER_SUPPORTS_CXX0X) 
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x ") 
else() 
     message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") 
endif() 

add_subdirectory (src) 

add_subdirectory (test) 

CMakeLists2.txt

cmake_minimum_required (VERSION 2.8.11) 

add_library (Feedparser news.h xml2json.hpp jsonxx.cc curler.cc feed.cc) 

target_link_libraries(Feedparser pthread) 
target_link_libraries(Feedparser curl) 

install(TARGETS Feedparser 
     RUNTIME DESTINATION bin 
     LIBRARY DESTINATION lib 
     ARCHIVE DESTINATION /usr/lib) 
install(FILES "feed.h" DESTINATION /usr/include ) 
target_include_directories (Feedparser PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) 

CMakeLists3.txt

cmake_minimum_required (VERSION 2.8.11) 

add_executable(Feedtest test.cc) 

target_link_libraries (Feedtest LINK_PUBLIC Feedparser) 

這是我的頭文件。

feed.h

#include "news.h" 
#include "curler.cc" 
#include "jsonxx.h" 
#include "jsonxx.cc" 
#include <iostream> 
#include <sstream> 
#include <stdlib.h> 
#include <thread> 
#include <stdio.h> 
using namespace std; 
using namespace jsonxx; 

class feed{ 

    map <string,string> info; 
    std::map<int, Object> items; 
    string url; 
    news News; 
    Object *item; 
    void strip_items(); 
    public: 
     Object json; 

     feed(string url); 

     void create(string url){ 
      this->url = url; 
     } 
     feed(){} 
     string get_topic(){ 
      return info["title"]; 
     } 
     bool fetch(); 
     bool fetch_data(); 
     bool parse(); 
     string get_url(){ 
      return url; 
     } 
     string get_item_title(int index){ 
      return News.title[index]; 
     } 

     string get_item_img(int index){ 
      return News.image[index]; 
     } 

     string get_item_link(int index){ 
      return News.link[index]; 
     } 

     int get_total(){ 
      return News.num_item; 
     } 

     struct news get_news(){ 
      return News; 
     } 



}; 

我應該包括在feed.cc feed.h和編譯和編譯器如何直接鏈接.h文件與檔案.CXX文件?

我該如何編寫一個cmake腳本來安裝這個庫? 我的錯誤在哪裏?

回答

0

C++的建設過程中由2個階段:

編譯:編譯器的每個源(.cc)文件,生成intermidiate二進制上運行。

鏈接:鏈接器在每個中間二進制文件上運行,匹配聲明和定義。最後,它將所有這些組合起來製作最終的二進制文件。


考慮到這一點,你需要確保編譯器不跨的東西,它不知道來了。這就是爲什麼你包含標題(.h)。

鏈接器應該滿足編譯器創建的所有內容。

+0

檔案的情況是一樣的嗎?我應該在.cc文件中包含.h文件嗎?還是我在外部編譯它們?對於將.h和.a文件分別移動到/ usr/include和/ usr/lib /的cmake文件必須做什麼修改? @IvanRubinson –