I want to use Unity for making unit test on my C code but I get the following error when compiling:
$ make
gcc ../Unity/src/unity.o src/helpers.o src/chess.o src/search.o test/TestCheck.o src/main.o -o checkai
src/main.o: In function `main':
main.c:(.text+0x4b): undefined reference to `UNITY_BEGIN'
main.c:(.text+0x55): undefined reference to `UNITY_END'
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: checkai] Error 1
$ make -n
gcc ../Unity/src/unity.o src/helpers.o src/chess.o src/search.o test/TestCheck.o src/main.o -o checkai
My main functon just looks like this:
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"
#include "chess.h"
#include "../../Unity/src/unity.h"
//#include "../test/TestCheck.h"
void test_Smoke(void) {
TEST_ASSERT_EQUAL_INT(1, 2);
}
int main() {
version();
chesspiece chessboard[8][8];
initField(chessboard);
UNITY_BEGIN();
RUN_TEST(test_Smoke, 1);
return UNITY_END();
}
Do I link something incorrectly? I already tried to change the order of the gcc command but nothing has helped yet.
When I comment out UNITY_BEGIN and UNITY_END I can compile it and the smoke test runs without problems.
I could solve it by not using the release version 2.1.0.
To solve it I have cloned the current master branch and it just worked even with UNITY_BEGIN and UNITY_END:
git clone https://github.com/ThrowTheSwitch/Unity.git
Related
I'm trying to compile a C program linking two previously created object files but keep getting an 'undefined reference' error.
I'm using Visual Code to write the code and Ubuntu on Windows to compile using a makefile. The two C files, task5.c and reverse.c which have been made into object files both contain #include reverse.h statements which contains the prototypes for the functions in reverse.c.
task5.c
#include <stdio.h>
#include "ctap.h"
#include "reverse.h"
TESTS {
const char *a = "Family";
char *b = reverse(a);
//test 1
ok(string_length(a) == string_length(b), "Strings are the same size");
//test 2
is("ylimaF", b, "Strings match");
}
reverse.c
#include <stdio.h>
#include <stdlib.h>
#include "reverse.h"
char *reverse(const char *str) {
//code
}
int string_length(const char *str) {
//code
}
reverse.h
char *reverse(const char *str);
int string_length(const char *str);
makefile
linked:
gcc -o linked task5.o reverse.o
task5.o
gcc -c -o task5.o task5.c
reverse.o
gcc -c -o reverse.o reverse.c
When I run the command make linked I expect it to be made and return nothing.
But when I run that command I get this error:
cc task5.o -o linked
task5.o: In function `ctap_tests':
task5.c:(.text+0x1abe): undefined reference to `reverse'
task5.c:(.text+0x1ace): undefined reference to `string_length'
task5.c:(.text+0x1adc): undefined reference to `string_length'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'linked' failed
make: *** [task5] Error 1
According to this GNU make documentation, the GNU make program will try to use GNUMakefile, makefile or Makefile.
The make program will not try makefile.mk, which means that e.g. make linked will use no makefile and only the default rules.
You can solve this by either renaming your makefile as Makefile (the most common) or makefile; Or by using the -f option to specify the makefile
$ make -f makefile.mk linked
I made a shared library as the follow:
gcc -c output.c
gcc -shared -fPIC -o liboutput.so output.o
When output.c is the follow, it could work.
//#include "output.h"
#include <stdio.h>
int output(const char* st) {
return 1+2;
}
But, when output.c changed as the follow, a error occur.
//#include "output.h"
#include <stdio.h>
int output(const char* st) {
printf("%s\n", st);
return 1+2;
}
This is error message:
/usr/bin/ld: output.o: relocation R_X86_64_PC32 against undefined 符号 `puts##GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: 最后的链结失败: 错误的值
collect2: error: ld returned 1 exit status
I want to know why and how to deal it. Thanks in advance.
You need to compile output.c as position independent code.
gcc -c -fPIC output.c
In the first version you have not called any library function. But in second one printf is being called. In general, compile all sources with -fPIC if you intend to build a shared library later.
I'm new to C and I keep running into the same error. I am typing the code in nano text editor and compiling it in the linux terminal. My operating system is Ubuntu 14.04. Here's an example of code that won't compile,
#include <math.h>
#include <stdio.h>
int main()
{
float x = 23.33f;
x = roundf(x);
printf("%f\n", x);
return (0);
}
The error I am receiving is,
cc example.c -o example
/tmp/ccWV0Or8.o: In function `main':
example.c:(.text+0x1d): undefined reference to `roundf'
collect2: error: ld returned 1 exit status
make: *** [example] Error 1
Any help is much appreciated, thanks!
Link with the math library:
cc example.c -o example -lm
The math library functions are not part of standard C library which is linked by default. So you need link it yourself.
There's an interesting thread about why it's not part of libc:
Why do you have to link the math library in C?
I know this question has a lot of answers, but I am always getting an error on gcc c1.c
c1.c:(.text+0x5): undefined reference to `f'
collect2: error: ld returned 1 exit status
no matter what I try.
This is c1.c
#include <stdio.h>
#include "c.h"
int main()
{
printf("F %d\n",f());
}
THis is c2.c
#include <stdio.h>
int f(void) {return 7;}
int main()
{
printf("S %d\n",f());
}
This is c.h
int f(void);
How could I get it working? Actually, I was getting this error in a big program that is modelled like this. I guess this is the way to do it.
(And yes, main should return 0).
You're going to have problems, since you have two different main functions defined, but the general way to do this is a link-time issue:
gcc -c c1.c
gcc -c c2.c
gcc c1.o c2.o
All you need to do is figure out which main you want, and remove the unwanted one.
You could do something like:
gcc -Dmain=blah -c c1.c
gcc -c c2.c
gcc c1.o c2.o
I am unable to compile the following simple C code and I don't know why.
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <math.h>
int main(){
double result;
result = cos(0.5);
printf("asin(0.5) is %f\n", result);
return 0;
}
The error message I receive after I try to compile is -
In function
'main':
test.c:(.text+0xlc): undefined reference to 'cos'
collect2: ld
returned 1 exit status
You need to link with the math library (-lm).
gcc -Wall -Wextra -o test test.c -lm
See this C FAQ.
In general whenever you get undefined reference error it's due to the compiler is not able to find your function definition. So it may be your function ( and you have not typed the spelling of function correctly so you will get this error ) or may be built-in function like you have encountered in this case.
to explore there are various library and their linking are necessary at the time of compilation
whenever you use math function use -lm ( l stands for link and m is for math )
in pthread built-in functions use -lpthread
and so on ...
In this case indeed use -lm
gcc -lm test.c
will be able to compile your program .