-1

I'll start by showing you the error I have been getting:

Henrys-MacBook-Pro-2:assignment1 HenryDashwood$  clang++ main.cpp
Undefined symbols for architecture x86_64:
  "clear()", referenced from:
      _main in main-a61991.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've been trying to move some functions into a source.cpp file with prototypes in a header.h file. The code works fine when I have it all in the main.cpp file. It also works when I move the function prototype to a header file. However, when I then also move the functions to the source file, it errors me! Here are the relevant bits of my code:

main.cpp

#include <string>
#include <iostream>
using namespace std;
#include "header.h"


int main()
{
  char quit, choice;
  int term, day, hour;
  string termName, dayName;

  clear();

  return 0;
}

header.h

#define TERMS 4
#define DAYS 7
#define HOURS 8

struct TTcell
{
    string subject;
    string lecturer;
    string roomName;
};

struct TTcell timetables[8][7][4];

void clear();

source.cpp

#include <string>
#include <iostream>
using namespace std;
#include "header.h"

void clear()
{
  for (size_t i = 0; i < TERMS; i++) {
    for (size_t j = 1; j <= DAYS; j++) {
      for (size_t k = 1; k <= HOURS; k++) {
        timetables[k][j][i].subject = "";
        timetables[k][j][i].lecturer = "";
        timetables[k][j][i].roomName = "";
      }
    }
  }
}

This is an example using one function to keep the question readable. They all seem to have the same affliction. I saw on other posts people got similar errors because of the compiler they were using. I've tried c++, g++ and clang++, all to no avail.

Thank you in advance for any ideas you come up with!

Henry Dashwood
  • 143
  • 1
  • 2
  • 13

2 Answers2

2

You have two options to make this compile.

Compile all the cpp files on one line

g++ main.cpp source.cpp -o main

Compile separately and link

g++ -c main.cpp
g++ -c source.cpp
g++ -o main main.o source.o
merlin2011
  • 71,677
  • 44
  • 195
  • 329
1

This is a bog-standard failure to bring in your source files, and has nothing to do with your compiler.

Henrys-MacBook-Pro-2:assignment1 HenryDashwood$  clang++ main.cpp

You didn't build & link source.cpp.

So, as far as Clang knows, the definition for clear() indeed does not exist.

Henrys-MacBook-Pro-2:assignment1 HenryDashwood$  clang++ main.cpp source.cpp
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Ah yes. Sorry. I see. I have actually tried compiling them together but it gave me different error which confused me a bit: Henrys-MacBook-Pro-2:assignment1 HenryDashwood$ clang++ main.cpp source.cpp duplicate symbol _timetables in: /var/folders/yl/c5z1v7897q3g9ywlj63pm0840000gn/T/main-1b3fd6.o /var/folders/yl/c5z1v7897q3g9ywlj63pm0840000gn/T/source-09f71d.o ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – Henry Dashwood Apr 27 '17 at 23:27
  • Sorry that's not very readable. I'm guessing there is a way to make code display nicely in comments! – Henry Dashwood Apr 27 '17 at 23:29
  • @HenryDashwood Two cpp files (translation units) include header.h. Than means two translation units define `struct TTcell timetables[8][7][4];`. Linker hates that. Put `extern struct TTcell timetables[8][7][4];` in the header to tell both files that `timetables` exists somewhere to be defined later and `struct TTcell timetables[8][7][4];` in one of the cpp files to fulfil the header's promise. More reading here: http://stackoverflow.com/questions/496448/how-to-correctly-use-the-extern-keyword-in-c – user4581301 Apr 27 '17 at 23:33
  • And on making code look nice in comments, no there is no way. That's on purpose to force people to put stuff like that in the question where it's easier to find by future readers with a similar problem. – user4581301 Apr 27 '17 at 23:36
  • Yes. It compiles now. Thank you for that. I'll go and read up on extern now! – Henry Dashwood Apr 27 '17 at 23:39
  • @HenryDashwood: Your C++ book explains it. Just keep reading. – Lightness Races in Orbit Apr 27 '17 at 23:40