undefined reference to `__lzcnt16? - c

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

Related

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

Putting error or warning message (extern variable)

My a.c file:
int sum(int a, int b) {
return a + b;
}
My b.c file:
#include<stdio.h>
extern int sum(int, int);
int main() {
printf ("%d", sum(2, 3));
return 0;
}
gcc a.c b.c -o output, working fine.
Let say tomorrow, I change the definition of "a.c file" sum function by increasing the argument to three. Like this,
int sum(int a, int b, int c) {
return a + b + c;
}
But forget to change the usage in b.c file (means I'm still using with two variable)
gcc a.c b.c -o output (doesn't give compilation error or warning mssg, printf gives wrong answer, obviously)
Now consider I'm working in huge set of c file and I cannot make new header file, because it will create unnecessary dependency problem which may take huge time to resolve.
What is the best way to throw error or even warning message in case the extern original definition is changed in terms of argument ?
Regards
What is the best way to throw error or even warning message in case the extern original definition is changed in terms of argument?
Neither compiler nor linker will object to that. You'll just find out at runtime (if you are lucky) when your program stops working.
If this was C++ then name mangling would allow the linker to reject such mal-formed programs. However, for C the linker only needs to find a symbol with the right name. It has no means of checking the signature.
Using header files is the accepted way to get the compiler to make sure you do things right. Repeating function declarations over and over throughout your program is usually a very bad idea. Whatever downsides you perceive to using header files pale into insignificance when compared to your proposed approach.
If you simply won't use header files, then you'll just have to always be right!
Normally editors like (SourceInsight,Sublime) have the options to browse the symbols. By using this option you can easily find function calls and prototype.
Compiler never generate warnings or error for your problem.Self contained header files are best option to avoid this situation.
The best thing to do is try to avoid "extern" and include the header file for sum(). Using header files and prototyping your functions will help the compiler catch these issues.
test.c:
#include <stdio.h>
#include "math.h"
int main(void)
{
printf("%d", sum(2, 3));
return 0;
}
math.h:
int sum(int a, int b, int c)
{
return (a + b + c);
}
output:
~]$ gcc test.c -o test
test.c: In function ‘main’:
test.c:6:5: error: too few arguments to function ‘sum’
printf("%d", sum(2, 3));
^
In file included from test.c:2:0:
math.h:1:5: note: declared here
int sum(int a, int b, int c)

Undefined reference in 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 ?

Share global variable between .c file

Hi I just wondering how to Share global variable between .c file.
I try to add follow code, but still get error.
test.c file
#include <stdio.h>
int max = 50;
int main()
{
printf("max %d", max); // result max 50
}
pass.h
extern int max;
passed.c
#include <stdio.h>
#include "pass.h"
max;
int main()
{
printf("pass %d \n", max);
return 0;
}
But when I compile passed.c I get follow error
Undefined symbols for architecture x86_64:
"_max", referenced from:
_main in passed-iOMugx.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Can anyone help? Thank you so much.
You can declare the variable in a header file, e.g. let's say in declareGlobal.h-
//declareGlobal.h
extern int max;
Then you should define the variable in one and only file, e.g. let's say, test.c. Remember to include the header file where the variable was declared, e.g. in this case, declareGlobal.c
//test.c
#include "declareGlobal.h"
int max = 50;
You can then use this variable in any file- just remember to include the header file where it's declared (i.e. declareGlobal.c), for example, if you want to use it in passed.c, you can do the following:
//passed.c
#include <stdio.h>
#include "declareGlobal.h"
#include "test.c"
int main()
{
printf("pass %d \n", max);
return 0;
}
The problem is that you have two programs, and data (like variables) can not be shared that simply between programs.
You might want to read about shared memory and other inter-process communication methods.
If on the other hand you only want to have one program, and use a variable defined in another file, you still are doing it wrong. You can only have one main function in a single program, so remove the main function from one of the source files. Also in pass.c the expression max; does nothing and you don't need it.
Then pass both files when compiling, like
$ clang -Wall -g test.c pass.c -o my_program
After the above command, you will (hopefully) have an executable program named my_program.

Including files in C

I want to make a simple function involving sqrt(), floor() and pow(). So, I included <math.h>. When I try to use my function, my program says that sqrt() and floor() do not exist. I've triple checked my files and rewritten them, but it still gives the same error. Just to check if there was anything wrong with the <math.h> directory, I made another separate file that calculated the same thing and it worked. I am clueless right now. What am I doing wrong?
The code of the non functioning program:
#include <math.h>
#include "sumofsquares.h"
int sumofsquares(int x){
int counter = 0;
int temp = x;
while(temp != 0){
temp = temp - (int)pow(floor(sqrt(temp)), 2);
counter ++;
}
return counter;
}
The working test file:
#include <stdio.h>
#include <math.h>
int main(void){
printf("%d", (int)pow(floor(sqrt(3)), 2));
}
the error is this
/tmp/ccm0CMTL.o: In function sumofsquares':
/home/cs136/cs136Assignments/a04/sumofsquares.c:9: undefined reference
to sqrt' /home/cs136/cs136Assignments/a04/sumofsquares.c:9: undefined
reference to floor' collect2: ld returned 1 exit status`
I am using runC on a virtual Ubuntu OS to compile
You're probably missing the -lm argument to gcc, required to link the math library. Try:
gcc ... <stuff> ... -lm
There are at least two C FAQs relevant to your problem:
14.3
13.26

Resources