-1

I am very new to C, so I apologize for this newby question.

I would like to use this source code in my project: http://base64.sourceforge.net/b64.c.

So, I include it in my test file:

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

int main()
{
   return 0;
}

However, main() is defined in b64.c as well, so upon compiling, I get:

test.c:4:5: error: redefinition of ‘main’
b64.c:495:5: note: previous definition of ‘main’ was here
test.c: In function ‘main’:
test.c:5:1: error: number of arguments doesn’t match prototype
b64.c:495:5: error: prototype declaration

What is the correct usage of this source file, or any? How do we correctly use it, or use functions defined in that file?

Edit: I understand that the problem is due to the duplicate definitions of main. I know there can only be one. My question is rather, won't every meaningful project need it's main method? Then why is there a main method defined in b64.c? Are we just supposed to delete this method from the source code? It seems odd that the source code doesn't just come ready to be included and used.

user2517777
  • 741
  • 2
  • 7
  • 9

7 Answers7

1

It is never a good idea to #include a C source file into your code. You can either copy the code from the other C source file into your code, or include the needed prototypes in your code and make a call to the functions, linking those after compiling them separately.

unxnut
  • 8,509
  • 3
  • 27
  • 41
  • What do you mean by 'linking them'? I understand including the prototypes/calling them/compiling it at command line or in make, but unsure of what you mean by linking them. – user2517777 Jul 01 '13 at 14:19
  • I do not know your environment, so I'll assume `gcc` (Microsoft Visual Studio works in a similar way). If you have two files (call them `foo.c` and `bar.c`), you can *compile* them using `gcc -c foo.c` and `gcc -c bar.c`. These generate the object files `foo.o` and `bar.o`. Then, you have to resolve the references from one file to the other using `gcc -o prog foo.o bar.o` where `prog` is your final executable. Let us say you have a function `square` defined in `bar.c` and called from `foo.c`. These steps will resolve the references, and last step will be called linking. – unxnut Jul 01 '13 at 14:26
0

When you are including that source file you are getting 2 main() declaration which is incorrect. So you have redefined "main" in this case.

Including .c into another .c file doesn't make sense. C files compile to .obj files, which are linked by the linker into the executable code , so there is no need to include one .C file into another. Instead, you can make a .h file that lists the functions and include that .h file

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
  • I know this main doesn't do anything, and that the duplicate definition is causing the errors. However, in any actual project, won't we need to define our own main method? And thus it will cause errors when we try to include this source code? – user2517777 Jul 01 '13 at 14:13
  • In any actual project, you wouldn't be trying to incorporate an actual third-party program, only third-party libraries. And something designed to function as a library isn't going to have its own `main()` method. (It also isn't going to be used by `#include`ing the source code into your project source.) – This isn't my real name Jul 01 '13 at 18:32
  • Thank you. This was the root of my problem, interpreting the given source code as equivalent to a small library. – user2517777 Jul 02 '13 at 15:00
0

you should use one of the two main functions. If you want a new main, write your main method in your file and remove it from the 'b64.c' file, if you want to use it from the 'b64.c' file remove your (empty) main.

Bjorn
  • 457
  • 1
  • 7
  • 22
0

If main is defined in b64.c either you cannot simply redefine it in your main source file.

What do you want is to use several functions from b64.c in your program. Delete the main function from it and create a header file where you protoype all of the functions in b64.c. After that, include this header file in your main program. Have a look at this short Wikipedia entry. It should give you an overview of the topic.

Besides this: It seems that you aren't very familar with C. Try out some tutorials and go on reading about C.

akluth
  • 8,393
  • 5
  • 38
  • 42
0

First of all, you must redeclare the .c file as a .h, and then you have to go through the source code and rename any duplicate methods or global variable names. The main method is the starting point of a program so there can only be one.

scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
0

(Usualy you dont include .c files, only .h) if you want the functions inside the "b64.c" you should delete the main function from "b54.c"!

G.w. Weil
  • 21
  • 1
  • 6
0

A C application needs one main() function. Your file b64.c looks like a self-sufficient C program so you don't need your test.c. Just compile and run b64.c.

Bruce
  • 2,230
  • 18
  • 34
  • Yes, but then how would I actually use b64.c's functions in my own projects? – user2517777 Jul 01 '13 at 14:20
  • Either remove the main function in b64.c and call the other functions in the module, or use b64.c as your starting point and add/substitute your functionality in main(). – Bruce Jul 01 '13 at 14:23