Not able to call an extern function - c

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");
}

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 */

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;
}

C error in linking library

I am begginer in C and I have a problem with linking .c and .h files into main.c
I tried to link it and it looks like this in main.c :
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "hangman.h"
#include "hangman.c"
#include <string.h>
#include <time.h>
#include <ctype.h>
int main(void)
{
char secret[20];
srand(time(NULL));
getWord(secret);
hangman(secret);
return 0;
}
and like this in .c file
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include "hangman.h"
int isWordGuessed(const char secret[], const char lettersGuessed[]){
int pom = 0;
int i,j;
for(i = 0; i < strlen(secret); i++){
for (j= 0; j < strlen(lettersGuessed); j++)
{
if(secret[i] == lettersGuessed[j])
pom = 1;
}
if(pom == 1){
pom = 0;
}
else{
return 0;
}
}
return 1;
}
and like this in .h file
#define WORDLIST_FILENAME "words.txt"
int isWordGuessed(const char secret[], const char lettersGuessed[]);
Program just throws an exception. It tells me:
hangman.o: In function isWordGuessed':
hangman.c:(.text+0x0): multiple definition ofisWordGuessed'
main.o:main.c:(.text+0x0): first defined here
#include "hangman.h"
#include "hangman.c"
Do not include .c source files in your program. The include directive will simply copy hangman.c in your main.c.

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>

extern declarations and header files in 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

Resources