extern declarations and header files in C - c

I was looking at this question here :
How do I use extern to share variables between source files?
followed the manual . but still I get Linker errors ...
Would love to get some help and explanation why it happens..
I have 2 .c files and one header file :
------check.h----
#ifndef check
#define check
extern int num;
#endif
----check.c----
#include "check.h"
#include <stdio.h>
int func(int x, int y)
{
int z = x+y;
return z;
}
void printnum()
{
num++;
printf("%d",num);
}
----ynnynyny.c----
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include "check.h"
#include "check.c"
int num = 10;
int main()
{
printf("num before is : %d\n",num);
printnum();
printf("num now is : %d",num);
getchar();
return 0;
}
I keep getting these errors :
1> ynnyny.c
1> check.c
1> Generating Code...
1>ynnyny.obj : error LNK2005: _func already defined in check.obj
1>ynnyny.obj : error LNK2005: _printnum already defined in check.obj
I wrote the #ifndef stuff and also the extern declaration, so what Is the problem?
thanks!

don't include "check.c" in ynnynyny.c

Related

How to link main.c to another C file from different directory in Xcode Project

In the Xcode project with languages as C.
heap
heap_sort.c
heap_sort.c
main.c
this is the heap_sort.c file
#include "heap_sort.h"
int sum(int a, int b)
{
return a + b;
}
this is the heap_sort.h file
#ifndef heap_sort_h
#define heap_sort_h
#include <stdio.h>
#endif /* heap_sort_h */
this is the screenshot for my code in Xcode. I just wanted to import a function name sum to my main.c file.
this is the main.c
#include <stdio.h>
#include "heap/heap_sort.h"
int main(int argc, const char * argv[]) {
int s;
s = sum(3,5);
printf("The integer is: %d\n",s);
return 0;
}
Your main file doesn't have a declaration for sum, so it doesn't know how to call it properly.
This declaration should go in your header file:
#ifndef heap_sort_h
#define heap_sort_h
int sum(int a, int b);
#endif /* heap_sort_h */

Code::Blocks : Functions (and functions prototype) with the pointer FILE parameter get an error

So I was doing some basic C then I'm blocked.
I wanted to count lines in a file. Function and prototype function are in .c (for the function) and in .h (for the prototype function), the problem is I get errors
Note : I use the compiler GNU GCC C++ 14 ISO
From Function.c :
#include "Functions.h"
int FileLineCount(FILE *file)
{
rewind(file);
int iCount = 0;
char string[MAX_CHARACTER_SIZE] = "";
while((fgets(string, MAX_CHARACTER_SIZE, file) != NULL))
{
iCount++;
}
return iCount;
}
From Function.h :
#ifndef __FUNCTIONS_INCLUDED__
#define __FONCTIONS_INCLUDED__
int FileLineCount(FILE *file);
#endif
From the main.c :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Functions.h"
#define MAX_CHARACTER_SIZE 256
int main()
{
FILE *WordsFile = NULL;
const char strWorldFileName[] = "RandomWords.txt";
WordsFile = fopen(strWorldFileName, "r");
if(WordsFile == NULL)
{
printf("Error ! Impossible to open the file %s\n", strWorldFileName);
printf("Verify if this .txt file is in the folder where the file main.c is in it\n");
return EXIT_FAILURE;
}
printf("Line count : %i\n\n", FileLineCount(WordsFile));
}
Here is the error from the compiler :
error: unknown type name 'FILE'
error: unknown type name 'FILE'
#include <stdio.h>
in your Functions.h file
and fix this to be the same 'functions' and 'fonctions'
#ifndef __FUNCTIONS_INCLUDED__
#define __FONCTIONS_INCLUDED__

C: Include a file which includes a file that also needs to be included in the main program

I have a header file (generalfunctions.h):
#ifndef GENERALFUNCTIONS_H
#define GENERALFUNCTIONS_H
//functionsdeclartion for example
int getInt(char* text);
#endif /* GENERALFUNCTIONS_H */
and a C file generalfunctions.c where I include this headerfile (so I can use some of the functions within each other and don't have bother with their order) and code out the functions.
generalfunctions.c:
#include "generalfunctions.h"
#include <stdlib.h>
#include <stdio.h>
//functions implentaion for example
int getInt(char* text){
int i;
printf("%s\n", text);
if(scanf("%d", &i)==EOF){
printf("INT_ERROR\n");
exit(1);
}
while (fgetc(stdin) != '\n');
return i;
}
//...
Now I need some of these functions in a file called project_objects.c that together with project_objects.h defines a couple of structs, unions, variables and functions with these things I need for my project.
project_objects.h:
#ifndef POINT_H
#define POINT_H
typedef struct point{
int x;
int y;
} point;
point create_point(void);
void print_point(point *p);
//...
#endif /* POINT_H */
project_objects.c:
#include <stdlib.h>
#include <stdio.h>
#include "project_objects.h"
#include "generalfunctions.h"
point create_point(void){
point p;
p.x=getInt("Give my a x");
p.y=getInt("Give my a y");
return p;
}
void print_point(point *p){
printf("x: %d\n", p->x);
printf("y: %d\n", p->y);
}
//..
However I also need some of the functions described in generalfunctions.h directly in my main program:
#include "generalfunctions.c"
#include "project_objects.c"
#include <stdlib.h>
#include <stdio.h>
int main(void){
int i=getInt("How many points would you like to create?");
while(i<1){
i=getInt("Cannot create a negative number of points. How many points would you like to create?");
}
point pointarray[i];
for(int j=0; j<i; j++){
pointarray[j]=create_point();
}
for(int k=0; k<i; k++){
printf("Point %d:\n", k+1);
print_point(pointarray+k);
}
return EXIT_SUCCESS;
}
This seems to work. If I just include the h-files than I get the error that getInt() isn't defined when I link. And before when I included the C file for general functions in project_object.c I got errors for duplication. But now the files seem more dependent on each other than I planned. I also don't understand why this works.
Do not include .c-files. Write function protytypes in .h-files and include them.
project_object.h
typedef int faa;
foo.h
include "project_object.h"
faa foo( faa x ); // prototype for function "foo"
foo.c
#include "foo.h"
faa foo( faa x ) // implementation of function "foo"
{
return x + 666;
}
main.c
#include "project_object.h"
#include "foo.h" // include .h-file with prototype of function "foo"
int main( void )
{
faa x;
x = foo(0); // call function "foo"
return 0;
}

Not able to call an extern function

I want to call a function that reside in my func.c file into my main.c
This is my func.c
#include "func.h"
int func(int i) {
return ++i ;
}
This is my header file func.h
int func(int i);
And this is my main code: main.c
#include <stdio.h>
#include <stdlib.h>
#include "func.h"
main()
{
int res = func(3);
printf("%d\n",res);
system("pause");
}
Compiling the main code I get the error: undefined reference to 'func'
My goal is to call function from an external file (that is not part of the same compilation unit).
How to do it? Thank you!
#include <stdio.h>
#include <stdlib.h>
#include "func.h" // change ( since func.h is not a slandered header file its a user define header file)
main()
{
int res = func(3);
printf("%d\n",res);
system("pause");
}

fatal error: stdio.matrixVector: File or directory not found

I'm trying to compile a C method
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
void myMethod(double vector[], double matrixVector[])
{
int n = sizeof(matrixVector)/sizeof(double);
gsl_matrix *X = gsl_matrix_calloc(n, 3);
gsl_vector *Y = gsl_vector_alloc(n);
gsl_vector *beta = gsl_vector_alloc(3);
for (int i = 0; i < n; i++) {
....
}
....
}
but I get this error
fatal error: stdio.matrixVector: File or directory not found
How could I fix this?
1- all the include are wrong
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
should be
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_multifit.h>
note that 3 include need the GNU Scientific Library, make sure to have installed it before compile the code
2- in the loop
for (int i = 0; i < n; i++) {
you have to declare i outside of the loop (if you aren't in C99 mode).
You need to change the #include lines, you've replaced all of the .h suffixes with .matrixVector in what I assume was a bulk replace for the rest of the file.
Change:
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
To:
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_multifit.h>

Resources