2012-10-17 120 views
0

我有以下目錄結構:CMake的:未定義的參考功能

├── build (empty dir) 
    ├── CMakeLists.txt 
    ├── easylogging++.h 
    ├── help.h 
    ├── operation.h 
    ├── operation_list.h 
    ├── operations 
    │   ├── CMakeLists.txt 
    │   ├── matchcount.cc 
    │   └── matchcount.h 
    └── ops_toolkit.cc 

而且我是新來的CMake,並努力寫出的CMakeLists.txt。 我的matchcount.h有一個簽名,其實現在matchcount.cc中(如典型的C/C++結構)。 以下是我的基本目錄

cmake_minimum_required(VERSION 2.6) 
project(ops_toolkit) 

add_subdirectory(operations) 

add_executable(ops_toolkit ops_toolkit.cc) 
set(CMAKE_CXX_FLAGS "-std=c++0x") 

和下面的CMakeLists.txt是

include_directories(${ops_toolkit_SOURCE_DIR}/operations) 
link_directories(${ops_toolkit_BINARY_DIR}/operations) 
set(all_operations_HEADER operations/matchcount.h) 
set(all_operations_SOURCES operations/matchcount.cc) 

我得到了函數簽名未定義引用一個在操作目錄下名爲int matchcount(int, const char**),並笙歌以下

dev:~/work/ops_toolkit/build$ make 
Scanning dependencies of target ops_toolkit 
[100%] Building CXX object CMakeFiles/ops_toolkit.dir/ops_toolkit.cc.o 
Linking CXX executable ops_toolkit 
CMakeFiles/ops_toolkit.dir/ops_toolkit.cc.o: In function `__static_initialization_and_destruction_0(int, int)': 
ops_toolkit.cc:(.text+0x249): undefined reference to `operations::matchcount(int, char const**)' 
collect2: ld returned 1 exit status 
make[2]: *** [ops_toolkit] Error 1 
make[1]: *** [CMakeFiles/ops_toolkit.dir/all] Error 2 
make: *** [all] Error 2 

有人可以幫助我嗎? 感謝

+1

你是否實現了operations :: matchcount(int,char const **)? –

+0

@LuchianGrigore還有一件事,我沒有聲明或定義這個簽名,我有operations :: matchcount(int,const char **)。是的,我已經在matchcount.cc – abumusamq

+0

執行它'char const **'與'const char **'相同。是matchcount一個項目? – Synxis

回答

0

的問題來自於add_subdirectory及其關聯CMakeLists.txt(一個在./operations),這是不正確,請參閱this SO question for the implications of the add_subdirectory command

爲了清楚起見,我還建議您將include_directorieslink_directories放在主CMake文件中。

+0

中定義,因此包含和鏈接主CMake文件中的目錄就足夠了,我們不必將它們包含在子目錄中(除非有第三個深度級目錄)?如果是,子CMake文件應包含哪些內容? – abumusamq

+0

子目錄應該用於子項目(只是一個普通的CMake文件,包含'project','include_directories'等等)或模塊(只在父範圍內的'set'變量,如源代碼,頭文件和其他配置) 。 – Synxis