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
Related
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.
#include <stdio.h>
#include <math.h>
int exp_for_level(int n) {
return (int)(100 * pow(n, 2.3));
}
int main(){
int x;
x = exp_for_level(6);
printf("%d", x);
return 0;
}
I receive the following error when I run this code on an online compiler
/tmp/cc28S7ML.o: In function exp_for_level':
main.c:(.text+0x19): undefined reference to `pow'
collect2: error: ld returned 1 exit status
How do I rectify this?
After I couldn't get it to work on the online compiler, I followed advice from some other threads on
The file is stored under a file grades.c on my mac
I've tried entering this
$ gcc - Wall - lm -o grades grade . c
into my terminal and i just get zsh error: command not found
Any ideas on what the issue is here too?
The online compiler I'm using is
https://www.tutorialspoint.com/compile_c_online.php
EDIT: in my post, in main I'd miswritten the function as exp_to_level instead of exp_for_level. Didn't copy paste the entire code as it's too long. I narrowed it down and retyped it to the portion that yields the error.
There are some errors in your code, you have defined a function exp_for_level but you use exp_to_level.
Then your x variable is not defined
If you fix your code like this:
#include <stdio.h>
#include <math.h>
int exp_for_level(int n) {
return (int)(100 * pow(n, 2.3));
}
int main(){
int x = exp_for_level(6);
printf("%d", x);
return 0;
}
and you compile:
gcc -Wall powtest.c -o powtest -lm
it works.
About the error on the online compiler:
The undefined reference error occurs because you are missing -lm linker option.
Edit the online compiler command clicking on Project->Compile Options:
About this problem on your local machine:
After I couldn't get it to work on the online compiler, I followed
advice from some other threads on The file is stored under a file
grades.c on my mac I've tried entering this
$ gcc - Wall - lm -o grades grade . c
into my terminal and i just get zsh error: command not found
you don't have the compiler installed.
You should install clang, Have a look to this question
First of all your function name is wrong in the main take a look here exp_for_level
and in main its exp_to_level change one of them then also add int x in main to solve the issue.
I have a main code wich uses some libraries and I been compiling it like this:
gcc importedCFile1.c importedCFile2.c mainCode.c -O3 -lm -Wall -o maincode -lrt
Now I have added CUDA code in mainCode and changed its extension to .cu... So, how can I compile the whole thing?
I tried:
nvcc importedCFile1.c importedCFile2.c mainCode.cu -o maincode
but I got a lot of "undefined reference" to my functions in the imported C files.
To include my C files I am using:
extern "C" {
#include "importedCFile1.h"
#include "importedCFile2.h"
}
And ´importedCFile1.c´ is using some functions and variables declared in ´importedCFile2.c´ and ´mainCode.cu´. Like this:
extern int **se; // Variables from mainCode
extern int *n;
extern int numNodes;
extern int *getVector(int n); // Function from mainCode
extern int iRand(int high); // Function from importedCFile2
This functions are the cause of the undefined references. What should I do?
Also, how do I add the flags I need for the C code, such as -lrt, O3, lm and Wall??
EDIT: You can find a reduced example of the problem here:
https://github.com/mvnarvaezt/cuda/tree/master/minimalExample
If you compile the mainCode.c and importedCFile.c with gcc it works fine. If you compile mainCode.cu and importedCFile.c with nvcc you will get an undefined reference to anExample() (the function in importedCFile.c).
And you comment the header importing importedCFile.c and the call to anExampled() function it would work find.
Your problem is that the C code in importedFile.c is trying to call back C++ functions in mainCode.cu.
In order to be callable from C, C++ functions must have C linkage. Declare getVector() as
extern "C" int *getVector(int n) {
in mainCode.cu, and your example will compile fine.
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.
I have this code which I'm trying to run with GCC-TDM 1.7.4-2 using -msse4.2 (I tried msse4) with an error:
sse_lzcnt.c|7|warning: implicit declaration of function '__lzcnt16'|
sse_lzcnt.c|9|warning: too many arguments for format|
obj\Debug\sse_lzcnt.o||In function `main':|
sse_lzcnt.c|7|undefined reference to `__lzcnt16'|
An undefined reference is usually a linking error due to a missing lib file (.a ending) but the intrinsics headers don't need one?
I made sure the intrinsics headers are in the correct include directory. Heres the code,
#include <x86intrin.h>
#include <stdio.h>
int main()
{
unsigned short __X = 256;
unsigned short RESULT = __lzcnt16(__X);
printf("result: ", RESULT);
return 0;
}
You need to use the gcc command line option: -mlzcnt