What am i doing wrong.

Tadd.h

#ifndef ADD_H_GUARD
#define ADD_H_GUARD

int add(int a, int b);

#endif

Tadd.cpp

int add(int a, int b)
{
	return a+ b;
}

Taddmain.cpp

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

using namespace std;

int triple(int x) 
{
	return add(x, add(x,x));
}

int main()
{
	int xi;
	xi = triple(3);
	
	cout<<xi;
	cin.get();
}

I compile Tadd.cpp and Taddmain.cpp.

how do i make an exe file.

in geany. error on build.

g++ -Wall -o "Taddmain" "Taddmain.cpp" (in directory: /home/***/Programming/C codes)
/tmp/ccZzj327.o: In function `triple(int)':
Taddmain.cpp:(.text+0x86): undefined reference to `add(int, int)'
Taddmain.cpp:(.text+0x95): undefined reference to `add(int, int)'
collect2: ld returned 1 exit status
Compilation failed.

What do i need to do.

Comments
for use of -Wall

You can compile everything as separate files BUT to get an executable you need to link all the files required.
e.g. in three command do this:

g++ -Wall -c Taddmain.cpp
g++ -Wall -c Tadd.cpp
g++ -Wall -o Taddprog Tadd.o Taddmain.o

or in one coamnd

g++ -Wall -o Taddprog Taddmain.cpp Tadd.cpp

There are other ways, including making a library. If you are interested you can read about it in the gcc manual or post further questions here.

However, very well done for using -Wall (warnings all) you will benefit greatly from this, and I have added to your reputation for that because I guess about 1/3 of the questions here would be self-solved if other did that.

Hi,

use g++ -o Taddprog Taddmain.cpp Tadd.cpp
You will get your code compiled.

Regards,
Raj

This article has been dead for over six months. Start a new discussion instead.