Bes Dollma
Bes Dollma

Reputation: 383

undefined reference to `` CLion

I am trying to compile a project in the CLION IDE and I can't succeed but I can compile the exact same files in Eclipse. First this is the error:

CMakeFiles\Besart.dir/objects.a(library1.cpp.obj): In function `Init':
C:/Users/Besart/ClionProjects/Besart/Data Structures 1/HW 2/library1.cpp:17: 
undefined reference to `DataStructure::DataStructure()'

Now I have included the requested files, for example in the source file of library1.cpp I have:

#include "library1.h"
#include "DataStructure.h"
void* Init() {
    DataStructure* DS(nullptr);
     try {
       DS = new DataStructure();
      } catch (std::bad_alloc &e) {
       delete DS;
       return nullptr;
        }
      return (void *)DS;
}

but the file is implemented in DataStructures.cpp and declared in DataStructure.h See image!!

Finally this is the CMake:

cmake_minimum_required(VERSION 3.5)
project(Besart)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++11 )
set(SOURCE_FILES  "Data Structures 1/HW 2/main.cpp"
    "Data Structures 1/HW 2/Creature.cpp" "Data Structures 1/HW 2/Magi.cpp"
    "Data Structures 1/HW 2/DataStructure.cpp/"
    "Data Structures 1/HW 2/library1.cpp")

add_executable(Besart ${SOURCE_FILES}  "Data Structures 1/HW 2/main.cpp"
    "Data Structures 1/HW 2/Creature.cpp"
     "Data Structures 1/HW 2/Magi.cpp"
    "Data Structures 1/HW 2/DataStructure.cpp/"
     "Data Structures 1/HW 2/library1.cpp")

CMAKE

Can someone see the problem and help me compile this project in CLion? Thank you

Upvotes: 0

Views: 2608

Answers (1)

drescherjm
drescherjm

Reputation: 10857

You have an errant / after DataStructure.cpp here:

set(SOURCE_FILES  "Data Structures 1/HW 2/main.cpp"
    "Data Structures 1/HW 2/Creature.cpp" "Data Structures 1/HW 2/Magi.cpp"
    "Data Structures 1/HW 2/DataStructure.cpp/"
    "Data Structures 1/HW 2/library1.cpp")

change this to:

  set(SOURCE_FILES  "Data Structures 1/HW 2/main.cpp"
        "Data Structures 1/HW 2/Creature.cpp" "Data Structures 1/HW 2/Magi.cpp"
        "Data Structures 1/HW 2/DataStructure.cpp"
        "Data Structures 1/HW 2/library1.cpp")

then fix your add_executable() in the following way:

add_executable(Besart ${SOURCE_FILES})

no need to repeat the content of ${SOURCE_FILES}.

Upvotes: 1

Related Questions