Cant Figure out Linking Programs

ok so im a begginner and im trying to figure out how to link my programs together and for one i get a (File directory cannot be found) for the #include <program1.cpp> and secondly after i figured out how to fix that i had to put the whole file location i was getting this

C:\Users\Josh\Desktop\C++\Final.cpp In function 'int main()':
10 5 C:\Users\Josh\Desktop\C++\Final.cpp [Error] redefinition of 'int main()'
5 0 C:\Users\Josh\Desktop\C++\Final.cpp In file included from C:\Users\Josh\Desktop\C++\Final.cpp
6 5 C:\Users\Josh\Desktop\C++\hello1.cpp [Error] 'int main()' previously defined here
13 9 C:\Users\Josh\Desktop\C++\Final.cpp [Error] 'hello1' was not declared in this scope


#include <iostream>
#include <program1.cpp>
#include <program2.cpp>
#include <program3.cpp>

using namespace std ;

int main()
{
Program1();
program2();
program3();

getchar () ;
return 0 ;
}
For your project files, use #include "" as that will first check the same directory or pathname you specified.
Do not directly include other source files. Instead, use header files.
For example:
1
2
3
4
5
6
7
//program1.h
#ifndef PROGRAM1_H  //these are known as include guards
#define PROGRAM1_H //They prevent duplication of code

void Program1(); //function prototype

#endif 

1
2
3
4
5
6
//program1.cpp
#include "program1.h"

void Program1(){
//...
}


Finally, all programs may only have one main function as that is the "interface," or the first function to be executed.
Last edited on
each program can have only one main function, which is the entry point for execution of program, looks like you are trying to include other programs with main functions:
1
2
3
#include <program1.cpp>
#include <program2.cpp>
#include <program3.cpp> 

you cannot do this, if u want to invoke some methods defined in some other file, try creating hpp files, in which u can define ur functions, include those hpp files in ur program, google hpp file creation for more details.
like this:

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>

using namespace std;

#include "second.h" // pp if you like

int main(void)
{
	secondfunc();
	second_value=3;
}


second.h:
1
2
3
/* second.h */
extern int second_value; // declare second_value as extern (actual declaration of it is in the .cpp)
int secondfunc(void);		// prototype for secondfunc 


second.cpp:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "second.h"

int second_value=0;
int secondfunc(void)
{
	cout<<"This is secondfunc()"<< endln;
}


hope that helps
Topic archived. No new replies allowed.