0

I'm testing the CLion IDE, and I'm attempting to write a minimal C++ program. Here's my code:

in main.cpp:

#include "classings.h"

int main() {
    classings s;
    s.doSomething();
    return 0;
}

in classings.h:

class classings {
public:
    void doSomething();
};

in classings.cpp:

#include <string>
#include <iostream>
#include "classings.h"

void classings::doSomething() {
    std::cout << "hei" << std::endl;
}

I have no clue why this gives me this error:

Undefined symbols for architecture x86_64:
  "classings::doSomething()", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm on OSX 10.10.

Henrik Hillestad Løvold
  • 1,213
  • 4
  • 20
  • 46
  • 3
    Your includes have nothing to do with it. "Undefined symbols" is a **linker** error -- you need to tell the linker which libraries to link in. – DevSolar Feb 13 '15 at 14:08
  • But I need no libraries to link this simple program. – Henrik Hillestad Løvold Feb 13 '15 at 14:09
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – sjdowling Feb 13 '15 at 14:10
  • @HenrikHillestadLøvold: Agreed, didn't really check your code. But still, it's the **linker** complaining. `classings::doSomething()` is defined in classings.cpp, and the linker balks in `_main`, defined in main.cpp. Are you sure your linker sees both translation units? You mention CMake. Is classings.cpp in your CMakeLists.txt? – DevSolar Feb 13 '15 at 14:13
  • To quote JetBrains: "Please note that the quality of EAP versions may at times be way below even usual beta standards." I would consider CLion completely unsuitable for a beginner. – molbdnilo Feb 13 '15 at 14:17
  • 3
    Actually, I actively discourage IDE's in general for beginners. You're doing too much battle with the IDE specifics, without learning enough about the *general* workings of things. IDE's are great for being productive 9-to-5, but for learning the ropes, go command line all the way. Knowing how a compiler / linker / makefile works before having it automated by an IDE. (Actually, I came out of the other end of the tunnel and am *more* productive today with Vim and a hand-crafted CMake setup. ;-) ) – DevSolar Feb 13 '15 at 14:23
  • I am not familiar with `CLion` but it appears that `classings.cpp` is not being added to the `add_executable` in your CMakeLists.txt for your application. – drescherjm Feb 13 '15 at 14:48
  • Definitely not a beginner -- I'm giving lectures in OOP (not C++, but Java) in Uni. I have a bigger project which I intend to build using CMake; the "testings" class is merely a minimal "project" to assure that my code is correct, and that the problem indeed lies with the IDE/compiler/linker. Thanks for any answers, after porting to xcode and llvm it does compile so it seems it's CLion then. – Henrik Hillestad Løvold Feb 13 '15 at 20:49
  • The problem is in your lack of understanding of cmake, not in CLion. – marco.m Feb 18 '15 at 23:00

1 Answers1

0

I think that your source files classings.h and classings.cpp are not included within your CMakeLists.txt.

If you open CMakeLists.txt, it should look something like this:

Incomplete CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4)
project(untitled)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp) # your other source files aren't listed
add_executable(my-program ${SOURCE_FILES})

You can fix the problem by including the new source files

Correct CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4)
project(untitled)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp classings.hpp classings.cpp) # manually listing all sources
add_executable(my-program ${SOURCE_FILES})

FYI, you can save yourself the hassle of editing the CMakeLists.txt every time you add/remove a new source file by using the file() or aux_source_directory() command. Here's an example:

set(SOURCE_DIRECTORY "src")
file(GLOB_RECURSE SOURCE_FILES "${SOURCE_DIRECTORY}/*.c"   "${SOURCE_DIRECTORY}/*.h"
                               "${SOURCE_DIRECTORY}/*.cc"  "${SOURCE_DIRECTORY}/*.hh"
                               "${SOURCE_DIRECTORY}/*.cpp" "${SOURCE_DIRECTORY}/*.hpp"
                               "${SOURCE_DIRECTORY}/*.cxx" "${SOURCE_DIRECTORY}/*.hxx")
add_executable(my-program ${SOURCE_FILES})

Or

aux_source_directory("src" SOURCE_FILES)
add_executable(my-program ${SOURCE_FILES})
maddouri
  • 3,737
  • 5
  • 29
  • 51