-1

the problem seems to be with the compiler I'm using though I'm fairly new to programming so I'm not sure how to mess with that(I'm using VSCode on Mac OSX)

This is my header:

#ifndef STICKMAN_H
#define STICKMAN_H

class Stickman{
public:
Stickman();
};
#endif

This is my source file:

#include "stickman.h"
#include <iostream>

using namespace std;

Stickman::Stickman(){
    cout << "Hello\n";
}

This is my main:

#include "stickman.h"
#include <iostream>

int main(){
   Stickman figure;
}

This is the ERROR message in the terminal:

Alexandres-MBP:Game alexandrecarqueja$ cd
"/Users/alexandrecarqueja/Desktop/Game/" && g++ main.cpp -o main && "/Users/alexandrecarqueja/Desktop/Game/"main
Undefined symbols for architecture x86_64:
"Stickman::Stickman()", referenced from:
 _main in main-d38641.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 6
    You only compiled `main.cpp` you didn't compile `stickman.cpp` https://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files – Cory Kramer Mar 19 '18 at 22:31
  • 1
    @Raindrop7 When you supply multiple `-o` options to `gcc`, it will ignore all but the last one. – sepp2k Mar 19 '18 at 22:39
  • I get the error I mentioned above if I try compiling either [main.cpp] or stickman.cpp or both. It just doesn't work, but if I run this code in CLion it works just fine. Also I should note that if I remove stickman.cpp and just write that code in the header it runs fine(but I'm told that's not a good programming practice) – Alexandre Carqueja Mar 19 '18 at 22:47
  • Your `main` has a return type of `int`, so put in a `return` statement. – Thomas Matthews Mar 19 '18 at 23:10
  • @ThomasMatthews `return 0;` is implied if not explicitly added in `main()`. It's the programmer's choice whether to add it or not. – eesiraed Mar 19 '18 at 23:25
  • @FeiXiang: I'm aware of the exception for `main`. I would like the OP to get in the habit of matching a return type to return statements in functions. – Thomas Matthews Mar 19 '18 at 23:31
  • @ThomasMatthews I guess it's up to debate whether you should explicitly write `return 0;` in `main()`, although one should get compiler errors if they return the wrong type or have a `return` statement missing. – eesiraed Mar 19 '18 at 23:35

3 Answers3

4

You need to call this instead:

g++ main.cpp stickman.cpp -o main

which will also compile stickman.cpp. Then the linker will know what to do. Right now you have a #include stickman.h in your main which declares the class, but does not define it.

The linker sees a constructor is declared (in stickman.h), but does not see how it is implemented (stickman.cpp was not compiled). Hence it is not able to link with the constructor body.

BobMorane
  • 3,870
  • 3
  • 20
  • 42
  • Thanks, that really fixed the issue. Now, I'm not sure if you have any experience with VSCode(if not that's fine you can ignore what I'm about to say), but basically I have an extension that compiles and runs the code (called code runner) and it compiles by default only the main, what I find weird is that in projects where I have other .cpp files included, or headers that have the implementation code written in them, even though it just compiles the main everything still works fine, do you know why that would be? And how I could make this extension compile everything needed automatically? – Alexandre Carqueja Mar 20 '18 at 10:50
  • Quick update, I found that if I do #include "stickman.h" and #include "stickman.cpp" in the main file the Extension will compile properly, however I'm not sure if this is a good programming principle as it might get confusing when programs get bigger. Do you recommend this approach or see any problem it might pose in the future? – Alexandre Carqueja Mar 20 '18 at 11:01
  • Yes, because then everything is copy/pasted (through `#include`) in `main.cpp`. But beware of this practice: https://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header – BobMorane Mar 20 '18 at 11:37
  • Thanks, that post was great, understood it perfectly! – Alexandre Carqueja Mar 20 '18 at 11:45
0

It must be compiler specific because I ran the code in Visual Studio and it built successfully. I would suggest you get the free express/community Visual Studio 2017 IDE software if you happen to have a Windows computer. The code looks fine so I'm personally unsure of what may be causing your issue if it's not compiler related. If you only have a Mac computer, then I suggest maybe looking into other free compilers.

Sirius
  • 73
  • 1
  • 4
  • thanks for the reply, I'm also pretty sure this is just some kind of issue with VSCode's compiler(the IDE I'm using right now) bcs I ran it on CLion and it worked just fine, I just happen to prefer VSCode, but I've been having some problems with it lately and this one was just really confusing for me, as I have no idea what the error message means. – Alexandre Carqueja Mar 20 '18 at 00:55
0

You also receive this error in vscode if your project has a path that includes spaces. As mentioned above you also need to compile all your cpp-files.

To do this in vscode in e.g. macOS Catalina please see my answer here https://stackoverflow.com/a/61331301/1071899

Basically you need to make a tasks.json file with the compiler specific flags. Here you need to include that all *.cpp files should be compiled AND you need to escape the whitespaces by adding "\"${workspaceFolder}\"/*.cpp", instead of "${file}",. Take note of the two \". This will make sure that your project path is surrounded by "" and it will not complain about linker errors.

Simon Bøgh
  • 801
  • 8
  • 13