Undefined reference in C - c

I have include unsigned short crc_message(unsigned int key, unsigned char *message, int num_bytes); in my "data.h"
But when I try to use it in another code file
...
#include "data.h"
unsigned short crc16 = crc_message(XMODEM_KEY, buff, nread);
...
I always get
In function main':/h/u8/g3/00/g3helios/p33/g3helios/a2/packetize.c:57: undefined reference tocrc_message'collect2: ld returned 1 exit status
Can someone tell me why? Thanks!

I think you have to find crc_message() function in some library regarding crc and compile your program against it - for example if the library is called libcrc.so you have to do:
gcc -lcrc ...

1.In header file data.h you have provided the prototype of function crc_message.
2.But, your problem is undefined reference error during linking stage.
3.So,did you define the function crc_message anywhere in your source ?

Related

undefined reference to `function' in only 1 file c,

i have a c file named main.c
#include <stdio.h>
extern int main(int argc, char* argv[], char* envp[]);
void start(){
...;
int return_code = main(argc, argv, envp);
exit(return_code);
}
you can see I declared main but when using ld to link it:
$ (use ld to link, I didn't write it down because it's quite verbose and irrelevant)
ld: bin/test.o: in function `start':
/home/user/Desktop/test/test.c:28: undefined reference to `main'
make: *** [Makefile:49: link] Error 1
so what should i do (sorry if this is a simple question for you)
In C you have to define a main function that will be called automatically by your program, this is the base of your code.
I saw that you include "stdio.h" which is a library allowing to have access to some function like for example in my program the function "printf".
If you don't need it then don't include it :)
For example here is how to make your first program with a main function.
#include <stdio.h>
int main(int argc, char *argv[]){
... // Your code
printf("Hello world"); // Just print on your terminal this string
return (0); // 0 is the default return code if there is no errors
}
Generally speaking, invoking ld yourself is being a glutton for punishment. Use your C compiler to link until proven otherwise.
gcc -o bin/test bin/test.o will link a C program for you.
It looks like you tried to "fix" it by providing _start yourself. You can't (in C). _start is not a function.

Compiling C programs with static files

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

NaCl XSalso20 C code not compiling

I was just trying out the Xsalsa20 code from NaCl website. I am unable to compile it.
#include "build/BlackDragon/include/x86/crypto_stream.h"
#include<stdio.h>
int main(){
//const unsigned char m[crypto_stream_xsalsa20;
const unsigned char k[crypto_stream_xsalsa20_KEYBYTES] = "1234567890123456789012345678901";
const unsigned char n[crypto_stream_xsalsa20_NONCEBYTES] = "12345678901234567890123";
unsigned char c[51];
unsigned long long len;
len = 50;
crypto_stream_xsalsa20(c,len,n,k);
//printf("%s",m);
return 0;
}
When I try to compile this with GCC I am getting the following error message.
/tmp/ccNaI8Z1.o: In function `main':
Test.cpp:(.text+0xbc): undefined reference to `crypto_stream_xsalsa20_ref'
/tmp/ccNaI8Z1.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2:error: ld returned 1 exit status
Could someone please help me trouble shoot this.
PS: This code is within the NaCl folder where I compiled NaCl
I solved this by installing the library into the system path.
For easy you can use
apt-get install libnacl-dev

undefined reference to `__lzcnt16?

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

No warning from gcc when function definition in linked source different from function prototype in header

I had a problem with a part of my code, which after some iterations seemed to read NaN as value of a double of a struct. I think I found the error, but am still wondering why gcc (version 3.2.3 on a embedded Linux with busybox) did not warn me. Here are the important parts of the code:
A c file and its header for functions to acquire data over USB:
// usb_control.h
typedef struct{
double mean;
short *values;
} DATA_POINTS;
typedef struct{
int size;
DATA_POINTS *channel1;
//....7 more channels
} DATA_STRUCT;
DATA_STRUCT *create_data_struct(int N); // N values per channel
int free_data_struct(DATA_STRUCT *data);
int aqcu_data(DATA_STRUCT *data, int N);
A c and header file with helper function (math, bitshift,etc...):
// helper.h
int mean(DATA_STRUCT *data);
// helper.c (this is where the error is obviously)
double mean(DATA_STRUCT *data)
{
// sum in for loop
data->channel1->mean = sum/data->N;
// ...7 more channels
// a printf here displayed the mean values corretly
}
The main file
// main.c
#include "helper.h"
#include "usb_control.h"
// Allocate space for data struct
DATA_STRUCT *data = create_data_struct(N);
// get data for different delays
for (delay = 0; delay < 500; delay += pw){
acqu_data(data, N);
mean(data);
printf("%.2f",data->channel1->mean); // done for all 8 channels
// printf of the mean values first is correct. Than after 5 iterations
// it is always NaN for channel1. The other channels are displayed correctly;
}
There were no segfaults nor any other missbehavior, just the NaN for channel1 in the main file.
After finding the error, which was not easy, it was of course east to fix. The return type of mean(){} was wrong in the definition. Instead of double mean() it has to be int mean() as the prototype defines. When all the functions are put into one file, gcc warns me that there is a redefinition of the function mean(). But as I compile each c file seperately and link them afterwards gcc seems to miss that.
So my questions would be. Why didn't I get any warnings, even non with gcc -Wall? Or is there still another error hidden which is just not causing problems now?
Regards,
christian
When each .c file is compiled separately, the only information the compiler knows is the function prototype you have given.
Because every file is compiled separately, there is no way the compiler process of main.c knows the definition of mean in helper.c is wrong.
After the .c file is compiled, the signature will be stripped, so the linker cannot know the mean is wrong either.
A simple fix is always include the interface .h file in the implementation .c file
// in helper.c:
#include "helper.h"
double mean(DATA_STRUCT *data);
Then the compiler process of helper.c will notice that inconsistent type and warn you.
A mean usually is a real value so double is ok. Here you define mean as returning double, but the prototype says int mean(...).
The only way gcc can be aware of the fact that there's a redefinition, is if the redefinition occurs for real... When you compile files separately likely the mean prototype is missing... it is not shown in your code fragment at least: you should include helper.h also into helper.c. Doing so, gcc -c helper.c must give you a warning. I have gcc 4.3.2, but I am almost sure it must be so also for the version you have. In the main, you just use mean, so here the gcc trusts what is said in helper.h. When you link, there is no more information about the size of arguments and returning value, and bad things happen (like reading an int as a double).
Another detail: you say you get NaN for an int of the struct... well, in the struct there's a double, and int can't be NaN!

Resources