Getting An Undefined Reference To A Properly Defined Global Variable - c

So in short, I have two .c files and a shared.h header file in the same directory.
This is shared.h:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
// declaring the number to factor and the the variable
factor as shared gloval variables
extern int n;
extern int factor;
This is pfact.c:
#include "shared.h"
int n;
int factor = 0;
// start main
int main(int argc, char **argv) {
// fork
// if child, exec to child
return 0;
}
This is child.c:
#include "shared.h"
int main(){
int m=0, k;
int status, child_exit_status;
pid_t pid;
int fd[2]; //every child will create another pipe to write to in addition to the pipe it inherits from its parent to read from
printf("n is %d\n", n);
// goes more code
return 0
}
What am I doing wrong? the global variable n is declared once in pfact.c, "externed" in the header file shared.h and then the header file is included in child.c
Thanks in advance!

Those two lines in child.c are useless, you can remove it
extern int n;
extern int factor;
This could help you understand why:
How do I use extern to share variables between source files?
Child doesn't know n so you can add it in global in child.c but it's certainly not why you are trying to do.
You can't compile two main, you should maybe rethink the way you do your program.

You need to link your objects together...
gcc -g -Wall -c child.c
gcc -g -Wall -c pfact.c
gcc -g -Wall -o pgm child.o pfact.o
Re: extern lines useless: Yes, they aren't needed in pfact.c; but it is good practice to #include the header with the declaration anyway, so the compiler can cross-check that everything matches.

Related

How can i run multiple files in C (visual studio code)?

I want to to run a simple program, using vs code, which includes three files: main.c item.c item.h.
I understand that I there is a way to link things together, but I don't know how. Can you explain me how to do it?
I've tried also to add the extension to make a project, but I didn't understand how to.
Here's the code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "item.h"
int main () {
int a = 2;
int b = 3;
int res;
res = prod(a,b);
printf("%d ", res);
return 0;
}
item.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "item.h"
int prod(int a, int b) {
return a*b;
}
item .h
#ifndef ITEM_H
#define ITEM_H
int prod(int a, int b);
#endif
I don't know if you are using Windows or Linux or Mac, I'm going to explain it for Linux but the method is similar for the others.
First of you go on VS Code, then you click on new file and rename it "makefile", then you write this:
link: item.o main.o
gcc item.o main.o -o programName
main.o:
gcc -c main.c
item.o:
gcc -c item.c
clear:
rm -f item.o main.o programName //this one is to delete files faster
Once you wrote the makefile you write in the terminal the command make and you get the executable file for your program.
However in item.c you aren't using any of the library you included, you only need to include item.h; last thing, I don't know why you are doing the #ifndef thing but it seems a waste.

Build RSA-Signature of a file with libgmp

I want to implement RSA-Signature where I can calculate the signature of a file and verify a signature of a file. Therefore I use the libary gmp.
But when I want to print the data of the file, it always prints 0 even though the file is not empty. Here is my code:
//compiled with gcc rsa-sig.c -O3 -Wall -l gmp -o rsa-sig
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <gmp.h> /* GNU Multi Precision library */
int main(int argc, char *argv[])
{
int r;
mpz_t modulus;
mpz_init(modulus);
r=mpz_set_str(modulus, "FFFF",16); //just an example modulus
mpz_t data;
mpz_init(data);
FILE * stream;
stream = fopen("file.bin", "rb");
r= mpz_inp_str(data, stream,16);
mpz_mod(data,data,modulus);
gmp_printf("%Zd\n",data);
}
I can't figure out why the output of that is 0. Maybe one of you guys have an idea.
Thanks!!

Using char in different files in C

I have 3 .c files main.c, fun1.c, fun2.c
char buff[50];//in fun1.c
char *arg; //in fun2.c
arg = strstr(buff, "001"); //in fun2.c
I want to print buff in fun2.c but it gives an error buff undeclared, even though I declared it in fun1.h as extern char buff[];
There are functions in fun1.c and fun2.c each
It is hard to say what is wrong with your particular program, but here is an example which links 2 .c files with one .h file.
1. A header file functions.h:
#include <stdio.h>
extern void func();
Where I use extern to provide definitions for another file.
2. Now, a functions.c file which uses this header file:
#include "functions.h"
void func() {
printf("hello");
}
This needs to #include the header file, and use the function void() to print a message.
3. Finally, a main.c file which links it all together:
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"
int main(void) {
func();
return 0;
}
Which also needs function.h as it uses func(). You then can compile the code as:
gcc -Wall -Wextra -g main.c functions.c -o main
You could also look into makefiles, which would reduce this long compilation line to simply make.

including c file includes by header file

I have those 3 files in my workspace:
1.h
#ifndef SOME_HEADER_FILE_H
#define SOME_HEADER_FILE_H
//code
#endif
1.c
#include <stdio.h>
#include "1.h"
//code
main.c
#include "1.h"
int main(){
printf("hello");
}
but in main.c printf is unidentified, is there a way to make it work while the relevant header is called in 1.c?
how can I link the 1.c and 1.h files?
edit: it's an academic assignment and I'm not allowed to make changes in the main and header.
You have included #include <stdio.h> only in 1.c, not in 1.h or main.c.
Obvious solution is to include it in main.c.
Because of the way the #include macro works (it expands the whole header file that you include at the line where you call the macro), you actually don't need to include stdio.h within main.c as long as stdio.h is included in a header file that main.c includes.
Hopefully this makes it clear:
main.c
#include "test.h"
int main()
{
printf("I can call this function thanks to test.h!\n");
return 0;
}
test.h
#include <stdio.h>
This will work just fine.
But this is not the same as being able to use a function that a .c file has access to based on its own #include definition just because you cross-compiled them. In that case the other.c file that calls #include <stdio.h> will get printf(), but main.c does not.
In this setup,
main.c
int main()
{
printf("I don't have any way to call this...\n");
return 0;
}
test.c
#include <stdio.h>
You will not have any way for main to know what printf() is, even if you cross-compile the two. test.c knows what printf() is but not main.
What you want is to have #include <stdio.h> in other.h, and then #include "other.h" in main.c.
But for future reference, this is probably poor practise as it should be immediately apparent what each file's requirements are so that you get a good sense of what its job is.
So here's what I would probably suggest as the best solution:
main.c
#include <stdio.h>
int main()
{
printf("I can call this all on my own.\n");
return 0;
}

How to move C functions into separate file?

I'm getting started with C programming. I currently have a large file that contains a lot of functions. I would like to move these functions to a separate file so that the code is easier to read. However, I can't seem to figure out how to properly include/compile and can't find an example in any online tutorials that I've found. Here's a simplified example:
#include <stdlib.h>
#include <stdio.h>
void func1(void) {
printf("Function 1!\n");
}
void func2(void) {
printf("Function 2!\n");
}
int main(void) {
func1();
func2();
return 0;
}
How do you move C functions into a separate file? FYI: I'm using gcc.
Update: These answers are very helpful, thank you. Now it seems that my simplified example is not good enough because I realized the reason my program failed to compile is because I'm using a global variable in my functions.
#include <stdlib.h>
#include <stdio.h>
int counter = 0;
void func1(void) {
printf("Function 1!\n");
counter++;
}
int main(void) {
func1();
return 0;
}
Moving these functions to an external file doesn't work because they need to reference this global variable:
#include <stdlib.h>
#include <stdio.h>
#include "functions.c"
int counter = 0;
int main(void) {
func1();
counter = 100;
return 0;
}
How can I get around this issue?
Okay. Here we go.
Your main.c file
#include <stdlib.h>
#include <stdio.h>
#include "functions.h"
int main(void) {
func1();
func2();
return 0;
}
Your functions.h file
void func1(void);
void func2(void);
Your functions.c file
#include "functions.h"
void func1(void) {
printf("Function 1!\n");
}
void func2(void) {
printf("Function 2!\n");
}
Compile it with:
gcc -o main.exe main.c functions.c
The most common way is to place your function prototypes in a header file and your function implementations in a source file. For example:
func1.h
#ifndef MY_FUNC1_H
#define MY_FUNC1_H
#include <stdio.h>
// declares a variable
extern int var1;
// declares a function
void func1(void);
#endif
func1.c
#include "func1.h"
// defines a variable
int var1 = 512;
// defines a function
void func1(void) {
printf("Function 1!\n");
}
func2.h:
#ifndef MY_FUNC2_H
#define MY_FUNC2_H
#include <stdio.h>
void func2(void);
#endif
func2.c:
#include "func1.h" // included in order to use var1
#include "func2.h"
void func2(void) {
printf("Function 2 with var1 == %i\n", var1);
}
main.c:
#include <stdio.h>
#include "func1.h"
#include "func2.h"
int main(void) {
var1 += 512;
func1();
func2();
return 0;
}
You would then compile using the following:
gcc -c -o func1.o func1.c
gcc -c -o func2.o func2.c
gcc -c -o main.o main.c
gcc -o myprog main.o func1.o func2.o
./myprog
I only placed one function in each source/header pair for illustration. You could create just one header which includes the prototypes for all of the source files, or you could create multiple header files for each source file. The key is that any source file which will call the function, needs to include a header file which includes the function's prototype.
As a general rule, you only want a header file included once, this is the purpose of the #ifndef #define #endif macros in the header files.
First you have to learn the difference between a declaration and definition. A declaration tells the compiler that something, like a function, exists. A definition is, for the case of functions, the actual function implementation.
So what you do is move the definition to another file, but add a declaration in the file where the function is to be called. You then build both files together, and the compiler and linker will take care of the rest.
You can do something like this.
/* func1.c */
void func1(void) {
printf("Function 1!\n");
}
/* func2.c */
void func2(void) {
printf("Function 2!\n");
}
/* main.c */
#include "func1.c"
#include "func2.c"
int main ( void )
{
func1();
func2();
return 0;
}

Resources