0

Okay, so I just started dabbing into c++ and I'm hoping to get ahead on my classes in the fall but instead of using the a compiler I've been using nitrous.io as an IDE. Anyways long story short, I'm using CMakeLists.txt to compile and run but I've been running into a lot of difficulties when trying to link together an interface, it's corresponding cpp file and a main function.

Gradebook.h

//Gradebook.h
 #include <string>
 using namespace std;

class Gradebook
{
  public:
    Gradebook(string n);
    Gradebook();
    void setCourse(string n);
    string getCourse();
    void displayMessage();

  private:
    string course;
}; 

Gradebook.cpp

//Gradebook.cpp
#include "Gradebook.h"
#include <iostream>
using namespace std;

Gradebook::Gradebook(string n)
{
  course = n;
} 

Gradebook::Gradebook()
{
  cout << "Default constructor used." << endl;
} 

void Gradebook::setCourse(string n)
{
  course = n;
}

string Gradebook::getCourse()
{
  return course;
}

void Gradebook::displayMessage()
{
  cout << "Welcome to " << course << "'s gradebook." << endl;
}

GradebookRunner.cpp - main function

#include "Gradebook.h"

int main()
{
  Gradebook g("Intro to c++");
  g.displayMessage();
  return 0;
}  

CMakeLists.txt - I'm 95% sure that the syntax and linking is done correctly in the .h and .cpp files, this is where I believe I'm making the mistake.

##I've tried to use different combinations of Gradebook and Gradebook.cpp in here.

cmake_minimum_required(VERSION 2.6)
project(Gradebook)
add_executable(Gradebook Gradebook.cpp)

Any help will be much appreciated :D

  • When I use GradebookRun.cpp in the CMakeLists file I get this error when compiling "GradebookRun.cpp:(.text+0x39): undefined reference to `Gradebook::Gradebook(std::basic_string, std::allocator >)' GradebookRun.cpp:(.text+0x5d): undefined reference to `Gradebook::displayMessage()' " – user3724652 Jun 10 '14 at 05:16
  • and when I use Gradebook.cpp in the CmakeLists file I get this error when compiling (.text+0x20): undefined reference to `main' – user3724652 Jun 10 '14 at 05:18
  • `add_executable(Gradebook Gradebook.cpp GradebookRunner.cpp)` – oblitum Jun 10 '14 at 05:22
  • When I change the preprocessor in GradebookRun.cpp to #include "Gradebook.cpp" it works perfectly, but that wasn't the way that the textbook showed. Fixed for now but would greatly appreciate any followup to getting the previous to work. – user3724652 Jun 10 '14 at 05:23
  • don't do that, try what I said. – oblitum Jun 10 '14 at 05:24
  • There's also [a GLOB feature](http://www.cmake.org/Wiki/CMake:How_To_Process_Lots_Of_Input_Files) that can add all cpps in a directory for compilation for example. – oblitum Jun 10 '14 at 05:28

1 Answers1

1

Simply add all files that belong to the executable (both headers and source files, so you have them all in IDE and dependencies are correctly resolved by CMake).

  add_executable(Gradebook Gradebook.cpp GradebookRunner.cpp Gradebook.h)

Note that GLOB feature could be tricky.

Community
  • 1
  • 1
Peter Petrik
  • 9,701
  • 5
  • 41
  • 65