C Programming: warning:conflicting types? - c

I'm getting an error when I compile my main.c
The error says:
functions.c:27:6: warning: conflicting types for 'secondfunction' [enabled by default]
functions.c:23:2: note previous implicit declaration of 'secondfunction' was here
(My program has a file called main.c, header.h, and functions.c)
main.c:
#include "header.h" /*Includes the header file*/
int main(void) /*Starts the main function.*/
{
course();
firstfunction(filename,fileextension,fp,MAX_SIZE);
/*
thirdfunction();
fourthfunction();
fifthfunction();
sixthfunction();
*/
return 0;
} /*Ends the main function.*/
header.h:
#include <stdio.h>
#include <string.h>
#include "functions.c"
#define MAX_SIZE 50
struct records
{
double euid;
char firstname[MAX_SIZE];
char lastname[MAX_SIZE];
float gpa;
};
void course(void);
void firstfunction(char filename[],char fileextension[],FILE* fp,int SIZE);
void secondfunction(FILE* fp);
struct records people;
FILE* fp;
char filename[MAX_SIZE],fileextension[MAX_SIZE];
functions.c:
void course(void)
{
printf("\n\n\n\n\nDepartment: CSCE\nCourse number: 1030\nProgram number: Homework 3\nName: xxxxxxxxxxxxxx\nEUID: xxxxxxxxxxxxx\nEmail: xxxxxxxxx\n\n\n\n\n");
}
void firstfunction(char filename[],char fileextension[],FILE* fp,int SIZE)
{
printf("Please enter the file name (ex:filename)\n"); /*Asks for the name and the extension*/
scanf("%s",filename);
printf("Please enter the file extension (ex:.txt)\n");
scanf("%s",fileextension);
strcat(filename,fileextension); /*Puts them together with strcat*/
printf("%s\n",filename);
fp=fopen(filename,"r");
secondfunction(fp);
fclose(fp);
} /*End of the first function*/
void secondfunction(FILE* fp)
{
} /*End of the second function.*/
Thanks for the help guys!!
I don't think it's the variables being passed to the function I think it's the fact that I'm calling a function from within a function. How do I fix this?

remove this line #include "functions.c" from header.h and include header.h in functions.c

You make use of secondfunction() in functions.c before you declare or define this function.
So compiler treats her as it usually does with unknown functions: assumes it takes variable number of int arguments and returns int.
When it next finds your definition - it yells about differences because your function returns void and takes FILE* as an argument.
Move your secondfunction() definition before firstfunction() and it will work. But what you should really do is read on what to put in header files. In your case put all functions declarations in header file, and problem will be gone, if you include header file on top of your functions.c

You declare both functions in header.h, which is great.
But in functions.c, when you call secondfunction from within firstfunction, no declaration or definition of secondfunction is yet visible.
You should add
#include "header.h"
to the top of functions.c. You should also add include guards to header.h so that you don't run into problems if it's included more than once:
#ifndef H_HEADER
#define H_HEADER
/* contents of header.h */
#endif
And as Mario's answer points out, you shouldn't include functions.c from anywhere. Except in unusual cases, only .h files should be #included.
Each function should be declared in a .h file, and defined in a .c file.
Each file that needs to refer to a given function should have a #include directive for the header that declares it.
(main needn't be declared in a header, since it's not normally called.)
As for the confusing "conflicting types" message, that's because of an obsolescent feature of C. In older versions of the language (prior to the 1999 standard), a call to an undeclared function would result in an implicit declaration of that function, and the compiler would assume that it returns a result of type int. Even if you're using a compiler that still does this (many still do by default), you should ensure that each function you call has a visible declaration.

Related

function declaration problem in multi file c project

I have a project that includes three files in codeblocks:
main.c :
#include <stdio.h>
#include <conio.h>
int global = 10;
void f1(int);
void f1_1(int);
void f2(void);
int main()
{
int x = 5;
printf("inside main file");
getch();
f1(x);
f2();
getch();
return 0;
}
file1.c :
#include <stdio.h>
#include <conio.h>
void f1(int x)
{
printf("\ninside file1 >> f1 and x = %i", x);
getch();
f1_1(x);
}
void f1_1(int x)
{
printf("\ninside file1 >> f1 >> f1_1 and x = %i", x);
getch();
}
file2.c :
#include <stdio.h>
#include <conio.h>
extern int global;
void f2()
{
printf("\ninside file2 >> f2 function , global var = %i", global);
getch();
}
When I compiled it, I got these warnings:
c|8|warning: implicit declaration of function 'f1_1'; did you mean 'f1'? [-Wimplicit-function-declaration]
c|11|warning: conflicting types for 'f1_1'
What should I do for this?
void f1(int);
void f1_1(int);
void f2(void);
Should be:
extern void f1(int);
extern void f1_1(int);
extern void f2(void);
Or, as #kiranBiradar points out, you can declare them in header files
file1.h
#pragma once
extern void f1(int);
extern void f1_1(int);
file2.h
#pragma once
extern void f2(void);
Note the use of the extern keyword. When you forward declare a function in a file as void f(void), the symbol 'f' is public, meaning other compilation units can reference it. When you declare it as extern void f(void), then the compiler doesn't expect the function to be defined in that compilation unit and leaves it up to the linker to find that symbol.
This is the perfect time to learn about the important concept of translation units.
Each single source file, with all included header file, forms a single translation unit. Each translation unit is separate and distinct and compiled stand-alone without any knowledge of other translation units.
That means symbols declared in e.g. the main.c source file will not be known in the file1.c source file.
When you compile file1.c the compiler simply doesn't know about the f1_1 function declaration you have in the main.c source file, so you get a warning about that fact.
To solve your problem, you need to declare the f1_1 function in the file1.c file. Either by adding a forward declaration (like the one you have in main.c), or moving the whole function definition (implementation) of f1_1 above the f1 function.
Or you could create a single header file which contains all the declarations needed (for the f1, f1_1, f2 function, plus the global external variable declaration), and include this single header file in all your source files. This solution works best if you have multiple symbols (functions, variables, etc.) that are used in multiple translation units.
My personal recommendation is this: Since the f1_1 function is only used internally inside the file1.c source file, move its definition above f1, and make it static. Then remove its declaration from the main.c source file.
Regarding the "implicit declaration" and "conflicting types" warnings, it's because in older standards of C it was allowed to not declare functions and the compiler would create an implicit declaration by guessing the declaration based on the first call of the function.
The important part about the guessing is that only the arguments were guessed, the return type would always be int.
I don't know the exact wording in the specifications about this since it was removed in the C99 specification, but most compilers still allow this with only emitting a warning instead of an error. This is where the first warning comes from.
However, the return type of int is still being used. And since your f1_1 function is declared to return void later in the file1.c source file, there's a mismatch between the guessed declaration (int f1_1(int)) and the actual declaration (void f1_1(int)) which leads to the second warning.

C - Function has internal linkage but is not defined

I have this folder structure:
I wrote a simple function to test the folder structure, for seen if all header file but when I compile with the make command, I have this error:
warning: function 'stampa' has internal linkage but is not defined
I the lagrange.h file I have this:
#ifndef LAGRANGE_H
static void stampa(int i);
#endif /* LAGRANGE_H */
and in the lagrange.c file I have this:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdio.h>
void stampa(int i){
printf("%d", i);
}
in the end, int main.c I simply call the function stampa passing them a number.
Explanation of the error:
The compilation error occurs because:
You have declared a function with static keyword (i.e. with internal linkage) inside lagrange.h
And you have included this file lagrange.h in main.c file different from lagrange.c, where the function is not defined.
So when the compiler compiles main.c file, it encounters the static declaration without any associated definition, and raises logically the error. The error message is explicit.
Solution:
In your case the solution is to remove the static keyword, because static means that the function can be called only in the .c file where it is defined, which is not your case.
Moreover, a good practice may be to declare any static function in the same .c file where it is defined, and not in .h file.

need a workaround for a "multiple definition" error

A toy code illustrating my problem is as follows:
stuff.h:
#ifndef STUFF
#define STUFF
int a;
int testarr[]={1,2,3};
#endif
fcn.h:
#include "stuff.h"
int b[]={5,6,7};
void fcn();
main.h:
#include "stuff.h"
#include <stdio.h>
fcn.c:
#include "main.h"
void fcn() {
printf("Hello\n");
}
main.c:
#include "main.h"
#include "fcn.h"
int main() {
fcn();
printf("HI\n");
}
An attempt to compile fails with:
/g/pe_19976/fcn_2.o:(.data+0x40): multiple definition of `testarr'
/g/pe_19976/main_1.o:(.data+0x40): first defined here
After doing some reading, I realize that defining the array testarr in the header file is a problem. But the thing is, in my real code, several files need access to testarr and it needs to have the same assigned values everywhere. I guess I could put it in main.h (?) but even if that would work, in my real code it logically belongs in stuff.h. How do I solve this conundrum?
BTW, based on something else I found, I tried defining testarr as extern but got the same problem.
When you put a variable definition into a header file, any .c file that includes it will have a copy of that variable. When you then attempt to link them, you get a multiple definition error.
Your header files should contain only a declaration of the variable. This is done using the extern keyword, and with no initializer.
Then in exactly one .c file, you put the definition along with an optional initializer.
For example:
main.c:
#include "main.h"
#include "fcn.h"
int a;
int testarr[]={1,2,3};
int main() {
fcn();
printf("HI\n");
}
stuff.h:
#ifndef STUFF
#define STUFF
extern int a;
extern int testarr[];
#endif
fcn.h:
#include "stuff.h"
extern int b[];
void fcn();
fcn.c:
#include "main.h"
int b[]={5,6,7};
void fcn() {
printf("Hello\n");
}
It is not clear why you are using so many global variables. The array
int testarr[]={1,2,3};
is defined as many times as there are compilation units (in your example there are at least two compilation units) that include the corresponding header.
Declare the array in a header like
extern int testarr[3];
and define it in a cpp module.
int testarr[]={1,2,3};
The same is valid for other global variables that have external linkage.
As for this remark
BTW, based on something else I found, I tried defining testarr as
extern but got the same problem.
Then the array with the specifier extern shall not be initialized in a header. Otherwise it is a definition of the array.

Duplicate symbol in C using Clang

I am currently working on my first "serious" C project, a 16-bit vm. When I split up the files form one big source file into multiple source files, the linker (whether invoked through clang, gcc, cc, or ld) spits out a the error:
ld: duplicate symbol _registers in register.o and main.o for inferred
architecture x86_64
There is no declaration of registers anywhere in the main file. It is a uint16_t array if that helps. I am on Mac OS 10.7.3 using the built in compilers (not GNU gcc). Any help?
It sounds like you've defined a variable in a header then included that in two different source files.
First you have to understand the distinction between declaring something (declaring that it exists somewhere) and defining it (actually creating it). Let's say you have the following files:
header.h:
void printIt(void); // a declaration.
int xyzzy; // a definition.
main.c:
#include "header.h"
int main (void) {
xyzzy = 42;
printIt();
return 0;
}
other.c:
#include <stdio.h>
#include "header.h"
void printIt (void) { // a definition.
printf ("%d\n", xyzzy);
}
When you compile the C programs, each of the resultant object files will get a variable called xyzzy since you effectively defined it in both by including the header. That means when the linker tries to combine the two objects, it runs into a problem with multiple definitions.
The solution is to declare things in header files and define them in C files, such as with:
header.h:
void printIt(void); // a declaration.
extern int xyzzy; // a declaration.
main.c:
#include "header.h"
int xyzzy; // a definition.
int main (void) {
xyzzy = 42;
printIt();
return 0;
}
other.c:
#include <stdio.h>
#include "header.h"
void printIt (void) { // a definition.
printf ("%d\n", xyzzy);
}
That way, other.c knows that xyzzy exists, but only main.c creates it.

Header/Include guards don't work?

For some reason, I'm getting multiple declarations of content within my header file even though I'm using header guards. My example code is below:
main.c:
#include "thing.h"
int main(){
printf("%d", increment());
return 0;
}
thing.c:
#include "thing.h"
int increment(){
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
int something = 0;
int increment();
#endif
When I attempt to compile this, GCC says that I have multiple definitions of the something variable. ifndef should make sure that this doesn't happen, so I'm confused why it is.
The include guards are functioning correctly and are not the source of the problem.
What happens is that every compilation unit that includes thing.h gets its own int something = 0, so the linker complains about multiple definitions.
Here is how you fix this:
thing.c:
#include "thing.h"
int something = 0;
int increment(){
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
extern int something;
int increment();
#endif
This way, only thing.c will have an instance of something, and main.c will refer to it.
You have one definition in each translation unit (one in main.c, and one in thing.c). The header guards stop the header from being included more than once in a single translation unit.
You need to declare something in the header file, and only define it in thing.c, just like the function:
thing.c:
#include "thing.h"
int something = 0;
int increment(void)
{
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
extern int something;
int increment(void);
#endif
The header guards will stop the file from being compiled more than once in the same compilation unit (file). You are including it in main.c and thing.c, so it will be compiled once in each, leading to the variable something being declared once in each unit, or twice in total.
try to avoid defining variables globally.
use functions like increment() to modify and read its value instead.
that way you can keep the variable static in the thing.c file, and you know for sure that only functions from that file will modify the value.
The variable something should be defined in a .c file, not
in a header file.
Only structures, macros and type declarations for variables and function prototypes
should be in header files. In your example, you can declare the type of something as extern int something in the header file. But the definition of the variable itself should be in a .c file.
With what you have done, the variable something will be defined
in each .c file that includes thing.h and you get a
"something defined multiple times" error message when GCC tries to link
everything together.
what ifndef is guarding is one .h included in a .c more than once. For instance
thing. h
#ifndef
#define
int something = 0;
#endif
thing2.h
#include "thing.h"
main.c
#include "thing.h"
#include "thing2.h"
int main()
{
printf("%d", something);
return 0;
}
if I leave ifndef out then GCC will complain
In file included from thing2.h:1:0,
from main.c:2:
thing.h:3:5: error: redefinition of ‘something’
thing.h:3:5: note: previous definition of ‘something’ was here

Resources