Build error: multiple definition of - c

I have a header file, wich contain a unsigned char array that is in fact a ttf font file.
In a .c file i'm able to load the font, and to use it ( i include the ttf header file in the .c file), but the problem is that when i'm trying to use this in another c file, i get one of those 2 errors;
First error that i can get say that my array is undeclared.
So i include the header file that contain my array, and i get the second error; Multiple definition of ...
Can someone help ? I dont understand.
I hope i'm clear in my explaination, and sorry for my approximative english :)

Use extern.
In your header file, make:
extern unsigned char my_array[];
In exactly one .c file, defined the array.

Related

How to automatically generate function headers for .h file in Clion?

When writing a library in C, I usually end up implementing functions before defining them in the corresponding header file. As copying function header's in the header file is a repetitive task, I was wondering if I can automatically generate function headers (or even better, the full header file) in CLion?
Maybe it's a little late (about 4 years), but here's the best way i've found (for a c file):
cut and paste the contents of the .c in the .h file, and for each function, put the cursor on it's name and press Alt+Enter, and choose "Split function into declaration and definition".
this will keep the declaration in the .h file while moving the implementation to the .c file.
hope it helps someone.
This only works for C++, but after you've written a function definition in a cpp file, eg:
void MyClass::myNewFn() { ... }
you can do Alt-Enter on myNewFn and select 'Create new function myNewFn()'. This will add a declaration of MyClass in the header file.

Including source files in C files

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.
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.
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.
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.
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.
(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"!
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.
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

C header <stdio.h>

I was trying to compile a 3-file C programme with 1 file cointaining tha main() , one containing headers related to a struct type I created and the third one the functions related to the struct. I was getting an error on the return temp; on a function in the third file, and I finally realized that I should remove the header in that file in order the functions the work properly, but I have no idea why that worked!!
Could someone please explain me why?
Check:
You put header guards in your header files
You don't have function or object definitions in your headers but only declarations.

stdio inclusion in a header file

I'm not totally sure but this looks wrong:
I have a header file named fraction.h in which I store a fraction structure and the methods to handle it, one method is used to write a fraction in a file and in the signature of this function one argument is a FILE pointer.
fraction.h:
...
const Fraction * fraction_fwrite(const Fraction * f, FILE * file);
...
fraction.c:
#include <stdio.h>
#include "fraction.h"
...
Now when I try to compile a program that uses a fraction, I get an error,
here is what I have in my Makefile:
program_test: fraction.o program_test.o
and I include fraction.h in program_test.c of course.
but I keep getting this error :
fraction.h:34:54: error: unknown type name 'FILE'
someone could explain the different steps through which the compiler includes files ?
because <stdio.h> is in fraction.c so why does it strike this unfound-type error ?
should I include <stdio.h> in fraction.h ? which looks not really appropriate from my measly experience.
When you compile program_test, the compiler isn't looking at the other .c files, only the files you #include in the file you actually compile.
So, you either have to #include <stdio.h> in the test program, just like in fraction.c, or include it in the header file.
Even though the C standard says that the standard library files will not include each other, there is nothing saying that user defined files cannot do that. In fact it is usually much easier to use them if they do.
When the preprocessor processes a file, then it will copy the content of an included file into the file that is preprocessed. The reason why you don't get that error in fraction.c is that the content of stdio.h is included before fraction.h is included.
So the preprocessed file fragments.c looks like this:
stdio.h contents
fragment.h contents
fragment.c contents
Notice that the FILE definition will be included through stdio.h before it is referenced in fragment.h.
You should include stdio.h in fraction.h to get rid of this error.
Include stdio.h in program_test.c. If you dont include stdio.h in program_test.c (which includes fraction.h), the compiler doesn't know the definition of FILE used by fraction.h, and generates error.
And, you really should include stdio.h in fraction.h

What do .c and .h file extensions mean to C?

It's all in the title; super-simple I reckon, but it's so hard to search for syntactical things anywhere.
These are two library files that I'm copying from CS50.net, and I'm wondering why they have two different extensions.
.c : c file (where the real action is, in general)
.h : header file (to be included with a preprocessor #include directive). Contains stuff that is normally deemed to be shared with other parts of your code, like function prototypes, #define'd stuff, extern declaration for global variables (oh, the horror) and the like.
Technically, you could put everything in a single file. A whole C program. million of lines. But we humans tend to organize things. So you create different C files, each one containing particular functions. That's all nice and clean. Then suddenly you realize that a declaration you have into a given C file should exist also in another C file. So you would duplicate them. The best is therefore to extract the declaration and put it into a common file, which is the .h
For example, in the cs50.h you find what are called "forward declarations" of your functions.
A forward declaration is a quick way to tell the compiler how a function should be called (e.g. what input params) and what it returns, so it can perform proper checking (for example if you call a function with the wrong number of parameters, it will complain).
Another example. Suppose you write a .c file containing a function performing regular expression matching. You want your function to accept the regular expression, the string to match, and a parameter that tells if the comparison has to be case insensitive.
in the .c you will therefore put
bool matches(string regexp, string s, int flags) { the code }
Now, assume you want to pass the following flags:
0: if the search is case sensitive
1: if the search is case insensitive
And you want to keep yourself open to new flags, so you did not put a boolean.
playing with numbers is hard, so you define useful names for these flags
#define MATCH_CASE_SENSITIVE 0
#define MATCH_CASE_INSENSITIVE 1
This info goes into the .h, because if any program wants to use these labels, it has no way of knowing them unless you include the info. Of course you can put them in the .c, but then you would have to include the .c code (whole!) which is a waste of time and a source of trouble.
Of course, there is nothing that says the extension of a header file must be .h and the extension of a C source file must be .c. These are useful conventions.
E:\Temp> type my.interface
#ifndef MY_INTERFACE_INCLUDED
#define MYBUFFERSIZE 8
#define MY_INTERFACE_INCLUDED
#endif
E:\Temp> type my.source
#include <stdio.h>
#include "my.interface"
int main(void) {
char x[MYBUFFERSIZE] = {0};
x[0] = 'a';
puts(x);
return 0;
}
E:\Temp> gcc -x c my.source -o my.exe
E:\Temp> my
a
They're not really library files. They're just source files. Like Stefano said, the .c file is the C source file which actually uses/defines the actual source of what it merely outlined in the .h file, the header file. The header file usually outlines all of the function prototypes and structures that will be used in the actual source file. Think of it like a reference/appendix. This is evident upon looking at the header file, as you will see :) So then when you want to use something that was written in these source files, you #include the header file, which contains the information that the compiler will need to know.
The .c is the source file and .h is the header file.
The .c files are source files which will be compiled. The .h files are used to expose the API of a program to either other part of that program or other program is you are creating a library.
For example, the program PizzaDelivery could have 1 .c file with the main program, and 1 .c file with utility functions. Now, for the main part of the program to be able to use the utility functions, you need to expose the API, via function prototype, into a .h file, this .h file being included by the main .c file.
.c : 'C' source code
.h : Header file
Usually, the .c files contain the implementation, and .h files contain the "interface" of an implementation.

Resources