This question already has answers here:
Linking static function from another file in c
(2 answers)
Closed 2 years ago.
I am getting a linking error when i run my program, even though the function signature is the same for both functions in test.h and test.c :
test.h :
void function(int);
test.c :
#include "test.h"
#include "stdio.h"
static void function(int n) {
printf("%d\n", n);
}
main.c :
#include "test.h"
int main() {
function(5);
return 0;
}
this is the output:
Undefined symbols for architecture x86_64:
"_function", referenced from:
_main in ccNaA2H2.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
In class, I learned that the function signature is the function name and its parameters.
So, why doesn't my program in main call function(5) in test.h which will call the function(5) in test.c ?
Thank you
static at global scope (outside of functions) means that it is only visible in this file (It has internal linkage). So, the static void function(int n) in test.c is not visible from main.c.
For the call function(5); in main, the compiler sees the function prototype in test.h, but the linker cannot find an implementation of it, because the function in the c file is static.
Solution: remove the word static, if you want to use the function in a different file.
In test.c you declared your function as static which means, it will not be visible outside that module. Remove the static keyword.
Related
This question already has answers here:
C error: undefined reference to function, but it IS defined
(6 answers)
Closed 9 months ago.
I have written a code that just basically add two numbers. file is the self made header file
named xoxo.h
extern int add(int r,int m);
This is my second file that contains the function defination of the function add.The name is
run.c
#include "xoxo.h"
int add (int i,int f) {
return (i+f);
}
This is my main file tester.c
#include "xoxo.h"
#include <stdio.h>
void main() {
printf("%d",add(1,2));
}
The error is shown as
PS C:\Users\HOME\Desktop\New folder> gcc tester.c
C:\Users\HOME\AppData\Local\Temp\ccBwWXFk.o:tester.c:(.text+0x1e):
undefined reference to `add' collect2.exe: error: ld returned 1 exit
status
plz help
When you compile the application, you need to provide run.c as well, otherwise the application cannot find the implementation of add.
Run gcc tester.c run.c instead.
This question already has answers here:
How do I use extern to share variables between source files?
(19 answers)
Closed 3 years ago.
I know this question has already been asked (in similar ways) several times, but I don't think it's a duplicate, because my code already implements the solution offered to the other questioners. If I overlooked something, my question may of course be marked as a duplicate and downgraded.
I have to use an external variable, because according to the task, I am not allowed to pass it as a parameter. The problem: If I want to compile the code, the "undefined reference"-error is thrown.
The code:
header.h
#ifndef TEST_HEADER_H
#define TEST_HEADER_H
extern int var;
void increment();
#endif //TEST_HEADER_H
source1.c
#include <stdio.h>
#include "header.h"
int main ()
{
int var = 1;
printf("1) %d\n", var);
increment();
printf("2) %d\n", var);
return 0;
}
source2.c
#include "header.h"
void increment()
{
var++;
}
The compile error:
====================[ Build | test | Debug ]====================================
/root/clion-2019.1/bin/cmake/linux/bin/cmake --build /root/CLionProjects/test/cmake-build-debug --target test -- -j 2
Scanning dependencies of target test
[ 33%] Building C object CMakeFiles/test.dir/source1.c.o
[ 66%] Building C object CMakeFiles/test.dir/source2.c.o
[100%] Linking C executable test
/usr/bin/ld: CMakeFiles/test.dir/source2.c.o: in function `increment':
/root/CLionProjects/test/source2.c:5: undefined reference to `var'
/usr/bin/ld: /root/CLionProjects/test/source2.c:5: undefined reference to `var'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/test.dir/build.make:100: test] Fehler 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/test.dir/all] Fehler 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/test.dir/rule] Fehler 2
make: *** [Makefile:118: test] Fehler 2
The CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(test C)
set(CMAKE_C_STANDARD 11)
add_executable(test source1.c header.h source2.c)
I also tried adding the following to the CMakeLists.txt, because it helped in another case of the "undefined reference"-error, but in this case it makes no difference.
find_library(LIBRT rt)
if(LIBRT)
target_link_libraries(test ${LIBRT})
endif()
I don't think the problem's in the code, is it? I think it's in the linking process. Can somebody help? Thanks in advance!
EDIT:
It runs now. My problem wasn't that I can't distinguish declaration and definition, as some assume, but that I set the definition in 'source1.c' in the wrong place within the 'main' (local) and not outside (global). So, to be precise, my misunderstanding was the estimation of the scope between an external declaration and its definition, if you understand what I mean. Admittedly, that wasn't a stroke of genius. ;-) Thank you for your help!
You misunderstand what extern means.
Marking a variable (or function) as extern does not define (create) that variable. Rather it just declares that it exists somewhere. You do not have a definition for var anywhere.
In your main you have int var = 1; but that definition is local to main. It is not global, and not accessible to any other functions.
You should move that variable definition out of main:
#include <stdio.h>
#include "header.h"
int var = 1;
int main ()
{
printf("1) %d\n", var);
increment();
printf("2) %d\n", var);
return 0;
}
On a related note: you should avoid using global variables. They lead to code that is difficult to understand, debug, and maintain.
This
extern int var; /* declaration, it doesn't exist until its not defined somewhere */
just a declaration, you need to define it. Since you didn't provide definition for var, linker is unable to locate it, hence the error
undefined reference to `var'
Note that in source1.c
int var = 1;
its not the definition of extern variable, its a locally created variable having functional scope.
I am trying to compile a c program with a static library and its not working .
This is the error :
undefined reference to `calculatearea'
collect2.exe: error: ld returned 1 exit status .
The static files were made with the gcc / g++ compilers .
This is the main code :
#include <stdio.h>
#include <stdint.h>
int calculatearea(int a , int b);
int main()
{
int c = calculatearea(2,4);
printf("%d",c);
getchar();
return 0;
}
edit :
: screenshot of compiler error
From the above code we can see that you have declared the function int calculatearea(int a , int b); but have not written any definition for the same. and you are calling this function in the main. compiler is not finding the definition for the function calculatearea and giving error.
To solve this:
1) Write the definition for function calculatearea in the same file.
2) Make use of extern specifier with this function declaration and make sure that definition is present with the link library at the time of compilation.
3) As mentioned in the picture if the area.o have the definition of function calculatearea, then compile as below, this will generate a.out in linux:
gcc filename.c area.o
This question already has answers here:
What is the difference between a definition and a declaration?
(27 answers)
Closed 5 years ago.
I have two .c files, one of them has the definition of x, and the other file is using x, as follows:
file1.c:
int x;
//other code...
main.c:
int main(void)
{
printf("%d", x);
}
Now, when I compile this code, I get the following compilation error message:
In function 'main':
error: 'x' undeclared (first use in this function)
So, if a global variable (in this case x) is 'extern' by default, then why can't main.c file see x?
When I now go to main.c and define x, so that main.c now looks like:
int x=9; //This line was added.
int main(void)
{
printf("%d",x);
}
And also initialize x in file1.c, the program doesn't compile and I get the following error message:
error: ld returned 1 exit status
So, if main.c can't see x that is in file1.c, then this time what is the problem?
Is this a linking error?
Note that when I add
extern int x;
in main.c, the problem disappears.
Each compilation unit (in this case your individual .c files) is compiled separately. The compiler needs to know the storage class of x in order to handle it, so your first error (undeclared) comes from the compiler not knowing what x is. The compiler does not need to know where x lives.
When you then link your compiled objects together, the linker resolves any external names (including x in main.c if you've marked it extern) and the final executable will then have all its variables in known places. If it finds 2 extern symbols with the same name, then it will fail, giving you your second error (error: ld returned 1 exit status).
You must declare you variable in main.c, so the compiler knows about it: extern int x. The compiler said it to you: error: 'x' undeclared
You added the second definition of x in main.c, the first definition you did in file1.c. The linker informed you about ambiguity between two definitions. You could read the error above the line error: ld returned 1 exit status
I have these two different program where I want to access the static variable declared in program1 from program2.
Program1. (
/* file a.c */)
#include<stdio.h>
static int a = 100; /* global static variable not visible outside this file.*/
int *b = &a; /* global int pointer, pointing to global static*/
Program2
#include<stdio.h>
/* file b.c */
extern int *b; /* only declaration, b is defined in other file.*/
int main()
{
printf("%d\n",*b); /* dereferencing b will give the value of variable a in file a.c */
return 0;
}
While I compile program1 , gcc a.c , no compilation error, but while I compile program2 ( gcc b.c) I am getting compilation error .
test_b.c:(.text+0x7): undefined reference to `b'
collect2: error: ld returned 1 exit status
Why there is compile error ? Here is the link of program static
Thanks in advance.
EDIT 1:
My intention to use static variable from other program. I thought every .c program must have main() function and only .h program have declaration , I am wrong at that point. So I remove main() function from a.c program and instead of compiling two different program separately , now I compile only once using gcc a.c b.c as per suggestion of Filip. Now it's working fine. Thanks all of you.
You have to link against a.c while compiling b.c:
gcc a.c b.c
You can't expect the linker to magically find the C file where b is defined. extern means it is defined elsewhere - you have to say where. By compiling and linking with a.c, the linker can now find a declaration for b.
Of course, you can't have 2 main() functions.
Well, your code already said it. b.cpp only has a declaration, not a definition, of the symbol in question.
Since these are clearly meant to be source files from two separate projects, I would suggest moving your definition to its own .cpp file which may then be shared between the two projects.
$ gcc a.c myIntPointerIsHere.c
$ gcc b.c myIntPointerIsHere.c
However, there are clearer ways to share code between two different projects.
The both modules contain the definition of main. It seems that the compiler did not include the first module in your project. Otherwise I think it would issue an error that main was redefined.