Importing a C function - c

What's wrong with the following import statement? I feel like I'm screwing up something pretty basic here:
// main.c
#include <stdio.h>
#include "1_square.h"
int main(int argc, char *argv[]) {
int a=4;
int a_squared=square(a);
printf("Square of %d is %d\n", a, a_squared);
return 0;
}
// 1_square.h
int square(int num);
// 1_square.c
int square(int num) {
return num*num;
}
$ gcc 0_main.c -o 0_main && ./0_main
/usr/bin/ld: /tmp/cc6qVzAM.o: in function main': 0_main.c:(.text+0x20): undefined reference to square'
collect2: error: ld returned 1 exit status
Or does gcc need a reference to 1_square in order to build it?

You need to add the 1_square.c file to your build command:
gcc -o 0_main 0_main.c 1_square.c && ./0_main
You need the definition of the function as well as its declaration.
From the comments:
Why then does it need 1_square.c in the commandline and then also 1_square.h in the 0_main.c header?
Like John Bollinger points out in the comments, the only thing 1_square.h and 1_square.c have in common is their name, which isn't meaningful to the compiler. There's no inherent relationship between them as far as gcc is concerned.
Let's start by putting everything in one source file:
/** main.c */
#include <stdio.h>
int square( int num );
int main( int argc, char **argv )
{
int a = 4;
int a_squared = square( a );
printf( "Square of %d is %d\n", a, a_squared );
return 0;
}
int square( int num )
{
return num * num;
}
In C, a function must be declared before it is called in the source code; the declaration introduces the function name, its return type, and the number and types of its arguments. This will let the compiler verify that the function call is written correctly during translation and issue a diagnostic if it isn't, rather than waiting until runtime to throw an error. In this particular case, we specify that the square function takes a single int parameter and returns an int value.
This says nothing about the square function itself or how it operates - that's provided by the definition of the square function later on.
A function definition also serves as a declaration; if both the caller and called function are in the same translation unit (source file), then you can put the definition before the call and not have to mess with a separate declaration at all:
/** main.c */
#include <stdio.h>
int square( int num )
{
return num * num;
}
int main( int argc, char **argv )
{
int a = 4;
int a_squared = square( a );
printf( "Square of %d is %d\n", a, a_squared );
return 0;
}
This is actually preferable as you don't have to update the function signature in two different places. It means your code reads "backwards", but honestly it's a better way to do it.
However, if you separate your function out into a separate source file, then you need to have a separate declaration:
/** main.c */
#include <stdio.h>
int square( int num );
int main( int argc, char **argv )
{
int a = 4;
int a_squared = square( a );
printf( "Square of %d is %d\n", a, a_squared );
return 0;
}
/** square.c */
int square( int num )
{
return num * num;
}
When it's processing main.c, the compiler doesn't know anything about the definition of the square function in square.c - it doesn't even know the file exists. It doesn't matter if main.c is compiled before square.c or vice-versa; the compiler operates on one file at a time. The only thing the compiler knows about the square function is that declaration in main.c.
Having to manually add a declaration for every function defined in the separate .c file is a pain, though - you wouldn't want to write a separate declaration for printf, scanf, fopen, etc. So BY CONVENTION we create a separate .h file with the same name as the .c file to store the declarations:
/** main.c */
#include <stdio.h>
#include "square.h"
int main( int argc, char **argv )
{
int a = 4;
int a_squared = square( a );
printf( "Square of %d is %d\n", a, a_squared );
return 0;
}
/** square.h */
int square( int num );
/** square.c */
int square( int num )
{
return num * num;
}
By convention, we also add include guards to the .h file - this keeps the contents of the file from being processed more than once per translation unit, which can happen if you #include "square.h" and include another header which also includes square.h.
/** square.h */
#ifndef SQUARE_H // the contents of this file will only be
#define SQUARE_H // processed if this symbol hasn't been defined
int square( int num );
#endif
Also by convention, we include the .h file in the .c file to make sure our declarations line up with our definitions - if they don't, the compiler will complain:
/** square.c */
#include "square.h"
int square( int num )
{
return num*num;
}
After both main.c and square.c have been compiled, their object code will be linked into a single executable:
main.c ------+-----> compiler ---> main.o ----+--> linker ---> main
| |
square.h ----+ |
| |
square.c ----+-----> compiler ---> square.o --+
We must compile both C files and link their object code together to have a working program. No doubt your IDE makes this easy - you just add source files to the project and it builds them correctly. gcc lets you do it all in one command, but you must list all the .c files in the project.
If you're running from the command line, you can use the make utility to simplify things. You'll need to create a Makefile that looks something like this:
CC=gcc
CFLAGS=-std=c11 -pedantic -Wall -Werror
main: main.o square.o
all: main
clean:
rm -rf main *.o
All you need to do then is type make at the command line and it will build your project, using built-in rules for compiling main.c and square.c.

Related

Is there any wrong about tha static variable?

I have a problem about a program. I bet that it has to do with the fact that I use static. Here is my t.h
static int cnt;
void f();
my main.c
#include <stdio.h>
#include "t.h"
void main()
{
cnt=0;
printf("before f : cnt=%d\n",cnt);
f();
printf("after f : cnt=%d\n",cnt);
}
and finally my f.c
#include "t.h"
void f()
{
cnt++;
}
The printf prints cnt=0 both times. How is this possible when I do cnt++? Any ideas?
Thanks in advance
In C, static means "Local to the module"
Take note, that the #include statements just pastes the header file in the including file.
therefore, you are creating two distinct symbols (happens to have the same logical name) in different modules.
f.c cnt is a different cnt then main.c
Note:
static in C has different meaning then its C++ counterpart.
and because C++ is C Compatible, static outside a class have the same meaning as in C
Edit:
In your case, you don't want a static you want a variable, but i guess you had problem with the Linker telling you about "ambiguous symbols".
I would suggest to declare an extern in the header file, and declare the actual variable in a module.
t.h
extern int cnt; // declaration of the variable cnt
main.cpp
#include
#include "t.h"
void main()
{
cnt=0;
printf("before f : cnt=%d\n",cnt);
f();
printf("after f : cnt=%d\n",cnt);
}
t.cpp
#include "t.h"
int cnt = 0; // actual definition of cnt
void f()
{
cnt++;
}
Data should not be defined in the header files.
In your example you will create a separate copy of that static variable in every compilation module which includes this .h file.
Don't define cnt in your header file. Instead, define it in f.c:
#include "t.h"
int cnt = 0;
void f(){
cnt++;
}
Then in main.c, add the following before the beginning of your main function:
extern int cnt;

TrueStudio - Why does the link static library fail?

I am using TrueStudio for my own stm32 project. I create 2 file foo.h and foo.c includes 2 functions
//foo.h
int add(int a, int b);
int sub(int a, int b);
and the implementation of timeout
//foo.c
#include "foo.h"
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
After that, I used gcc to compile a static library foo.a. I continue to make the main file to implement the library to test.
//main.c
#include <stdio.h>
#include "foo.h"
int main(int argc, const char *argv[])
{
int a = 100, b = 50;
printf("sum is: %d\n", add(a,b));
printf("sub is: %d\n", sub(a,b));
return 0;
}
Next, I link the static foo lib to main.c to make an executable file using command is
gcc main.c foo.a -o main
I ran it and get the result is
sum is: 150
sub is: 50
That's worked fine prove my static lib was built successfully.
I begin to create a project stm32 from stmcubeMX and linker to this foo.a and the error appeared.
undefined reference to 'add'
undefined reference to 'sub'
My full code and setting path and build bellow
//main.c in TrueStudio
#include "main.h"
#include "foo.h"
int main(void)
{
HAL_Init();
SystemClock_Config();
int a = 200, b = 100;
int _sum, _sub;
_sum = add(a, b);
_sub = sub(a, b);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
I am very grateful for any help, thanks!
Rename your foo.a file to libfoo.a, then change the C Linker -> Libraries -> Libraries to just foo with nothing in front or in the back. This should cause the final output to be -lfoo, which in turn causes linker to search for libfoo.a in the library search paths.

C static funtions accesible outside of file?

I used code blocks to make a project with main.c:
#include <stdio.h>
#include "t.h"
int main()
{
printf("%c\n", return_f('f'));
printf("%c\n", return_f(return_char(71)));
printf("%d\n", STATIC_INT);
return 0;
}
And t.h:
static int STATIC_INT = 14;
static unsigned char return_char(char n){
return (n/2 + 9);
}
unsigned char return_f(char n){
return ((n=='f' || n=='F') ? n+1 : 'f');
}
Since I assume static should limit globals and functions to their files, how does it allow to run/print out:
g
f
14
Or is that just not how it's supposed to work?
t.h is included textually before the actual compilation process takes place. Therefore static int STATIC_INT = 14; is part of your main.c file.
The real problem is that you are declaring variables in a header file which is almost always wrong.
It work because you import t.h in your .c file.
Static function can't be accesible outside of the file. But when you import t.h in your main.c file, all the code in t.h will be paste into main.c; so now your static function belong to main.c !
You have included t.h in your main.c, so these symbols are in the same unit of your main.c

Reading next line of a file with fscanf()

I am using two functions to read doubles(read_double) and integers(read_integer) from a file.
The file format is as follows excluding the characters in the ( )'s
12345678 (ID)
3.78 (GPA)
3 (Year)
20.5 (Age)
There are 5 of these entries in the file to be read.
#include <stdio.h>
double read_double(FILE *infile)
{
double double1=0.0;
fscanf(infile, "%lf",&double1);
return double1;
}
int read_integer(FILE *infile)
{
int int1=0;
fscanf(infile, "%d",&int1);
return int1;
}
int main(void)
{
FILE *inp,*outp;
double gpa1=0.0;
int id1=0;
inp=fopen("input.txt","r");
outp=fopen("output.txt","w");
id1=read_integer(inp);
gpa1=read_double(inp);
printf("%d, %.2lf",id1,gpa1);
return 0;
}
When this code runs it prints the Student ID which is 12345678, it then prints 1.00. This is due to it reading first the ID again.
How would I make this read the second line and return the GPA (3.78)
I need to split the program so that main is in one file, and the other functions are on another.
Note: I will have to do this 5 times. Also we have not "learned while statements or "gets()" so please try not to use these or more complex methods.
As per the comments, what you want is to split the program in library and main.
So, the library will have the two functions:
/* lib.c */
#include <stdio.h>
double read_double(FILE *infile)
{
double double1=0.0;
fscanf(infile, "%lf",&double1);
return double1;
}
int read_integer(FILE *infile)
{
int int1=0;
fscanf(infile, "%d",&int1);
return int1;
}
The header file will help programs that use your library know what to expect from the functions (what types should be used for their arguments and return values). It only contains prototypes, and not the actual function definitions:
/* lib.h */
#include <stdio.h>
double read_double(FILE *infile);
int read_integer(FILE *infile);
And main uses them:
/* main.c */
#include <stdio.h>
#include "lib.h" /* <== here! use the prototypes so
the compiler will know that
read_double returns double! */
int main(void)
{
FILE *inp;
double gpa1=0.0;
int id1=0;
inp=fopen("input.txt","r");
id1=read_integer(inp);
gpa1=read_double(inp);
printf("%d, %.2lf",id1,gpa1);
return 0;
}
And compile them:
gcc lib.c main.c -o program
Now, the input file is:
12345678
3.78
3
20.5
And we run the program:
./program
12345678, 3.78
Now, if we remove the #include "lib.h" from main
#include <stdio.h>
int main(void)
{
FILE *inp;
double gpa1=0.0;
int id1=0;
inp=fopen("input.txt","r");
id1=read_integer(inp);
gpa1=read_double(inp);
printf("%d, %.2lf",id1,gpa1);
return 0;
}
and compile it with optimization,
gcc -O3 lib.c main.c
Then the result is wrong:
./program
12345678, 1.0
because the compiler generated code assuming all functions return integers. And doubles and integers are represented in a totally different way, internally.

how to share a variable and its value between many source file in c language?

i have 2 source files (.c) named file1.c and file2.c which need to share between them a variable,
so that if in one source file the variable is been updated then in the other sourced file when accessing this variable the change will be seen.
what i did is create another source file called file3.c and header file called file3.h (which, of course, is been included in file1.c file2.c and in file3.c)
in file3.c:
int myvariable = 0;
void update(){//updating the variable
myvariable++;
}
int get(){//getting the variable
return myvariable;
}
in file3.h:
extern int myvariable;
void update(void);
int get(void);
in file1.c:
.
.
.
printf("myvariable = %d",get());//print 0
update();
printf("myvariable = %d",get());//print 1
.
.
.
in file2.c:
.
.
.
printf("myvariable = %d",get());//print 0 but should print 1
.
.
.
but the problem is when in file1.c update is invoked and myvariable is updated
the change cannot be seen in file2.c because in file2.c when get is invoked and
myvariable is printed then 0 is been printed, only if in file2.c update is invoked then the change is been seen.
it seems like the variable is shared but for each source file there is a different variable value/different memory for this variable
You can declare the variable as extern in the others file when you need the variable ...
include file3.h in file1.c and file2.c
I recommend to avoid extern variables because of code clutter - repeating externs in each and every file using that global. It is usually better to bind global variable to some file scope by making it static. And then to use interface functions to access it. In your example terms it will be:
// in file3.h
void update(int x);
int get(void);
// in file3.c:
static int myVariable = 0;
void update(int x){
myVariable = x;
}
int get(){
return myVariable;
}
// in other files - include file3.h and use
// update() / get() to access static variable
Here is just one possible solution. Doing this the the variable isn't global to the entire application and can only be read/written to using the access functions. Please let me know if you have questions.
Files: access.c access.h file2.c main.c
Compile with: gcc main.c file2.c access.c -o test
Run: ./test
File: main.c
#include <stdio.h>
#include "access.h"
int main( int argc, char *argv[] )
{
int value;
put( 1 );
printf("%d\n", get());
put( getValue() + 1 );
printf("%d\n", getValue());
return(0);
}
File: access.c
#include "access.h"
static int variable = 0;
int get( void )
{
return(variable);
}
void put( int i )
{
variable = i;
return;
}
File: file2.c
#include <stdio.h>
#include "access.h"
int getValue( void )
{
int i = get();
printf("getValue:: %d\n", i);
put(++i);
printf("after getValue:: %d\n", get());
return( i );
}
File: access.h
extern int getValue( void );
extern int get( void );
extern void put( int i );
And here is the run output:
[root#jrn SO]# ./test
1
getValue:: 1
after getValue:: 2
getValue:: 3
after getValue:: 4
4
I hope this helps.

Resources