Here are 2 simple files I am unsucessfully trying to combine and make the .exe. The errors i get are: Multiple definition of '_fun1' and '_fun2'.
I am using the Cygwin gcc compiler. Please advice, Thanks.

#include <stdio.h>
#include "functions.c"

int i = 35;
int fun1();
int fun2();

int main()
{
	printf ("\n %d", i);
	fun1();
	fun2();
	return 0;
}
/* functions.c */
#include <stdio.h>

extern int i;
int fun1()
{
	i++;
	printf ("\n %d", i);
	return 0;
}

int fun2()
{
	i--;
	printf ("\n %d", i);
	return 0;
}

How are you compiling?

Using "makefile" and also tried with Code::blocks.

What I mean is, show me the command you use to invoke gcc. I suspect you're doing something like this:

$ gcc main.c functions.c

This compiles both files to *.o files and then tries to link them, but during compilation the contents of functions.c are textually inserted into main.c, so you end up having two copies of functions.c when the linker takes over.

I am using the make command and here is what my makefile looks like.
Make just gives me an obj folder with 2 object files for both the source code files in it. This is what is specified in the book i am using.

TARGET = prog1.7
TOPDIR=$(KSET_TOPDIR)/c_x86

include $(TOPDIR)/makefile.inc

Don't use the second include line in the first file. You don't need that.

Use the extern keyword in the first file on lines 5 and 6: Since these functions are external to this file and are found in the second file.

And then compile your file on the command line as follows:

gcc -o function.exe functions.c funct.c

I don't know what you called both of your file names. Or I just didn't pick it up. But this compiled with gcc 4.5.2 without warnings and produced the following result:

35
36
35

The reason you have that error message of multiple definitions is because you have not used the extern keyword in the first file.

Change functions.c to functions.h , it should be a header file unless you have a very good reason not to.

Then include the functions.h in the first file and remove the #include <stdio.h> from the second file otherwise you will be pulling that in twice.

Then remove int fun1(); and int fun2(); from the first file -- that's called prototyping, done only when you call a function before it's been declared, in this case you declare it in the header file so any attempt to redeclare it will throw an error.

/* functions.h */

int i;

int fun1(){
  i++;
  printf ("\n %d", i);
  return 0;
}

int fun2(){
  i--;
  printf ("\n %d", i);
  return 0;
}
#include <stdio.h>
#include "functions.h"

int main(){
  i = 35; /* set, don't declare */	
  printf ("\n %d", i);	
  fun1();	
  fun2();	
  return 0;
}

Finally, compile with : gcc -o Program.exe functions.c

commented: Adds insightful comments suggesting improvement on user methodology. +5

Then include the functions.h in the first file and remove the #include <stdio.h> from the second file otherwise you will be pulling that in twice.

That's the preferred way to do things: insert the functions you wish to use in a header file, and then use an include statement. #include "MyHeader.h" User added headers not found or set in the INCLUDE environment path, need to be quoted using the actual path names. If the header is in the same path as the source file, then you can just use quotes. Otherwise #include "c:\My Headers\Myheader.h" But if the user doesn't want to use a header file, he needs to use the extern keyword on the prototype for the function. The compiler linker needs this so that it won't mangle the names. It searches and prevents compilation and linking of the files, if it finds duplicate declarations of variables, functions, etc.

Great!! Thanks for the help fellows :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.