0

I am trying to implement Header Files with Source Files using C++, but in the terminal the give me some errors.

Here is the code:

main.cpp

#include <iostream> #include "add.h" using namespace std; int main() { cout << "The Sum of 3 and 4 is: " << add(3,4) << endl; return 0; } 

add.cpp

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

add.h

#ifndef ADD_H #define ADD_H int add(int x, int y); #endif 

Terminal Messages: Terminal Messages

7
  • I'm voting to close this question as off-topic because no one would know how to 'implement Header Files with Source Files using C++' Commented May 24, 2016 at 19:15
  • First, you need to #include "add.h" from add.cpp. Second, the error message you pointed is apparently more related to the way you invoke clang than to the code itself. Commented May 24, 2016 at 19:18
  • We need to see the command you used that produced the error. Also please edit the command and the error message into the question. Commented May 24, 2016 at 19:19
  • 1
    @jpo38, That include isn't necessary in this case, but it's recommended. Commented May 24, 2016 at 19:20
  • 1
    Why are the errors from clang if you're using g++? Commented May 24, 2016 at 19:41

3 Answers 3

1

Try g++ -o add.o add.cpp followed by g++ -o HeaderTest main.cpp add.o.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code in C++ is perfectly fine. What you need is probably some adjustments in building. You should either read on how to build, i.e. compile and link manually or use some IDE, or at least build system like Make, CMake, QMake etc. If you want to do this by hand please provide command you have used to build this sample.

1 Comment

Thats the problem, i am trying to build it.
0

You did use the C-compiler. The warning tells you that you provide C++-code to the C-compiler. In case you want to get rid of this warning, try clang++ -o add.o add.cpp followed by clang++ -o HeaderTest main.cpp add.o

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.