0

I have following directory structure:

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

And I am new to CMake and trying to write CMakeLists.txt. My matchcount.h has a signature whose implementation is in matchcount.cc (as typical C/C++ structure). Following is my CMakeLists.txt in base directory

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")

and following is the one in operations directory

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)

I am getting undefined reference for function signature called int matchcount(int, const char**) and make complains following

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

Can someone help me with this? Thanks

arrowd
  • 33,231
  • 8
  • 79
  • 110
abumusamq
  • 780
  • 1
  • 10
  • 29
  • 1
    Do you implement operations::matchcount(int, char const**)? – Luchian Grigore Oct 17 '12 at 08:28
  • @LuchianGrigore Thats another thing, I do not have declaration or definition with this signature, i have operations::matchcount(int, const char**). And yes I have implementation of it in matchcount.cc – abumusamq Oct 17 '12 at 08:30
  • `char const**` is the same than `const char**`. Is matchcount a project ? – Synxis Oct 18 '12 at 16:26
  • @Synxis No, match count just contains bunch of functions project name is ops_toolkit as defined in root CMakeList – abumusamq Oct 18 '12 at 21:33

1 Answers1

0

The problem comes from the add_subdirectory and its associated CMakeLists.txt (the one in ./operations), which is incorrect, see this SO question for the implications of the add_subdirectory command.

I also advise you to put include_directories and link_directories in the main CMake file, for clarity.

Community
  • 1
  • 1
Synxis
  • 9,236
  • 2
  • 42
  • 64
  • so including and linking directories in main CMake file is enough and we dont have to include them in subdirectories (unless there is 3rd depth level directory)? if yes, what exactly should sub-CMake files contains? – abumusamq Oct 18 '12 at 21:35
  • Sub directories should be for sub-project (just a normal CMake file with`project`, `include_directories`, etc...) or modules (just `set` variables in parent scope, like sources, headers, and other config). – Synxis Oct 19 '12 at 06:10