0

I am a total beginner in C++ and I'm trying to learn how to add header files to the main. I have 3 files in the same folder (main.cpp, add.cpp, add.h)

main.cpp:

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

int main()
{
  std::cout << add(3,4);
  return 0;
}

add.cpp:

#include "add.h"

int add(int x, int y)
{
    return x + y;
}

add.h:

int add(int x, int y);

Whenever I run this from the atom g++ compiler, I get the following error: in function main':<br/>main.cpp:(.text+0x13): undefined reference toadd(int, int)' collect2: error: ld returned 1 exit status

Can someone tell me what mistake am I making?

Usitha Indeewara
  • 870
  • 3
  • 10
  • 21
Paris
  • 1
  • 1
  • You didn't link the two object files together. – tadman Oct 28 '22 at 22:40
  • perfect, how do i do that ? – Paris Oct 28 '22 at 22:43
  • 2
    Set up a build system. This isn't always easy, which is why a more integrated build environment helps considerably. Up to you though! There's many options from `make` to `cmake` to other even more exotic types. – tadman Oct 28 '22 at 22:43
  • Could you suggest another enviroment, or link a way to do it with make ? – Paris Oct 28 '22 at 22:46
  • For macOS XCode is a great place to start, or on Windows, and possibly Linux, there's Visual Studio Community. They can be a bit involved to get going with, but it's more a case of learning the UI than some arcane build macro system like you get with `make` or `cmake`. – tadman Oct 28 '22 at 22:47
  • Bit of handy reading: [How does the compilation/linking process work?](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work). If you know what the steps are it'll be easier to figure out how the steps map to the tools atom supplies. – user4581301 Oct 28 '22 at 22:48
  • `make` is a method that does work, but search for a "make template" that comes close to what you need, and work from there as a basis. – tadman Oct 28 '22 at 22:48
  • Check me on this because I'm old. Atom is a development system? – user4581301 Oct 28 '22 at 22:52
  • Its an IDE, but you guys helped a lot and I got it running on Visual Studio. – Paris Oct 28 '22 at 22:54
  • Groovy. There should be some configuration page that you can use to tell it to link together the files. With visual studio, if you create the file with VS, it automatically adds it. If you create the file with another tool, you have to add the file (right click and select add) in the solution explorer. – user4581301 Oct 28 '22 at 22:57
  • Atom is an extensible text editor. It is not an Integrated Development Environment. If it were, this question would not have been asked. – sweenish Oct 28 '22 at 23:52

0 Answers0