0

I have a pretty simple question about a C++ code which doesn't compile, possibly for some trivial oversight.

My project is structured as:

Project
|
+--->build
|
+--->core
|     |
|     +---->core.h
|
+---->core.cpp
|
+---->driver.cpp
|
+---->CMakeLists.txt

Listing out the files sans the business logic:

core.h

#ifndef CORE_H
#define CORE_H

class core
{

private: 
  int input;

public:
  void func1();

  inline void setInput(int input)
  {
    this->input = input;
  }     
};

#endif

core.cpp

#include "core.h"

void core::func1()
{ 
  std::cout << "Hi there!" << endl;
}

driver.cpp

#include "core.h"

int main()
{

  core c;
  int a = 10;

  c.setInput(a);
  c.func1(); 
}

CMakeLists.txt

project(Project)
cmake_minimum_required(VERSION 2.6)
set(header_search_path "core")
include_directories(${header_search_path})
add_library(lib_1 core.cpp)
target_include_directories(lib_1 PUBLIC ${header_search_path})

***Some additional stuff here for linking driver.cpp to external dependencies***

I build using

cmake ..
make -j8

After invoking the make command, I get the following error

CMakeFiles/driver.dir/driver.cpp.o: In function main': driver.cpp:(.text+0x8b8): undefined reference tocore::func1()' collect2: error: ld returned 1 exit status CMakeFiles/driver.dir/build.make:153: recipe for target 'driver' failed make[2]: * [driver] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/driver.dir/all' failed make[1]: * [CMakeFiles/driver.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

Any help would be appreciated.

If it helps, I am using cmake version 3.6.2 on Fedora 25.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
metsburg
  • 2,021
  • 1
  • 20
  • 32
  • you did not link the function func1 in the unit. add the .cpp as dependencies to your target – jonas_toth Mar 13 '17 at 17:42
  • 2
    The error `undefined reference to core::func1()'` indicates that you are not linking the definition of `core::func1()`. Make sure you are compiling and linking `core.cpp`. – François Andrieux Mar 13 '17 at 17:45

0 Answers0