I am trying to write a code in contiki that allows motes to randomly generate values.
Below is the code I tried:
#include "contiki.h"
#include "stdio.h" /* For printf() */
#include "stdlib.h"
PROCESS(random_process, "Random process");
AUTOSTART_PROCESSES(&random_process);
PROCESS_THREAD(random_process, ev, data)
{
PROCESS_BEGIN();
int r=rand();
printf("Hello, world. Random Number is %d",r);
PROCESS_END();
}
While generating the makefile I get the below error:
user#instant-contiki:~/Desktop/Random$ make target=native random_sample
TARGET not defined, using target 'native'
CC random_sample.c
LD random_sample.native
contiki-native.a(broadcast-annou): In function `set_timers':
/home/user/contiki-2.7/core/net/rime/broadcast-announcement.c:171: undefined reference to `random_rand'
collect2: ld returned 1 exit status
make: *** [random_sample.native] Error 1
rm random_sample.co
Can someone please help me with this? Thanks in advance.
You have not configured your project properly, you have to setup Makefile and project-conf.h to start with contiki, read the following hello-world example: http://github.com/contiki-os/contiki/tree/master/examples/hello-world.
I recommend you use the example in the link as a project start files.
Related
I'm trying to set a function pointer to point to the pow function.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void){
double (*func)(double, double) = pow;
return 0;
}
But the program doesn't compile.
I get this error:
$ gcc test.c -o test
/tmp/ccD6Pmmn.o: In function `main':
test.c:(.text+0x8): undefined reference to `pow'
collect2: error: ld returned 1 exit status
I'm using Ubuntu 15.10.
Anyone knows what's wrong with my code?
Thanks
You need to compile with -lm via command line or configure your IDE to add it into the linking process. This is due to the fact that some libraries are pretty large and to avoid taking up space in your program and compilation time, this was setup at the beginning of C when computers were much slower and would take MUCH more to compile and a matter of space was CRUCIAL.
I want write simple C program to set ACL to one particular file on Linux. My starting code is trying to use function from "acl.h". I'm using Ubuntu 13.10, I've installed "Access control list utilities" - "acl 2.2.52-1". Here is my code:
#include <sys/acl.h>
#include <stdio.h>
int main () {
acl_t aclvar;
int count = 1;
aclvar = acl_init(count);
return 0;
}
The problem is, that I get error while compiling with "gcc myAcl.c" or "gcc -lacl myAcl.c":
/tmp/cc5sVzSR.o: In function `main':
myAcl.c:(.text+0x15): undefined reference to `acl_init'
collect2: error: ld returned 1 exit status
How can I resolve this error?
The libraries you link to needs to come last
gcc myAcl.c -lacl
I'm pulling out my hair trying to figure out why this isn't working on my Minix system.
When I try to compile my C program, I get the following error:
#make
link pm/pm
program.o: In function `do_function':
program.c:(.text+0x1e): undefined reference to `shmget'
program.c:(.text+0x36): undefined reference to `shmat'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*** Error code 1
Stop.
make: stopped in /usr/src/servers/pm
#
This is my code:
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/stat.h>
/* Some other includes here */
typedef struct {
//struct elemLi *next;
//elem sem;
int ref_count;
} elemLi;
int do_function(void){
int segment_id; //Shared Memory ID
struct elemLi* sli; //Shared Memory Pointer
segment_id = shmget(IPC_PRIVATE, sizeof(elemLi),0660 | IPC_CREAT);
sli = (struct elemLi *)shmat(segment_id,NULL,0);
return -1;
}
As you can see, I've included the proper header files for these calls and it's still saying the references are undefined. I was able to successfully use this in another program, so I've completely run out of ideas as to why this isn't working in this instance.
Edit: This is within a system call. I assume that doesn't make a difference.
Thanks for the help everyone, it looks like it was a missing library in the linker. I added libc, using -lc, to the Makefile, and now it seems to be compiling fine.
When I compile this code, I get the following gcc errors:
/tmp/ccUigsI6.o: In function `main':
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:9: undefined reference to `setupterm'
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:10: undefined reference to `tigetnum'
/home/matt/Dropbox/school/2011/cs3210/test/sizeterm.c:11: undefined reference to `tigetnum'
collect2: ld returned 1 exit status
make: *** [sizeterm] Error 1
Here's the code:
#include <stdio.h>
#include <term.h>
#include <curses.h>
#include <stdlib.h>
int main()
{
int nrows, ncolumns;
setupterm(NULL, fileno(stdout), (int *)0);
nrows = tigetnum("lines");
ncolumns = tigetnum("cols");
printf("This terminal has %d columns and %d rows\n", ncolumns, nrows);
exit(0);
}
Libncurses is installed correctly on my machine. I get the same results from my Arch linux laptop, and the Ubuntu server installed at my school. This particular piece of code is taken directly out of the book. Am I doing something wrong? I've done some googling and it looks as though people have had this problem before, but I can't narrow down a solution.
You forgot to actually link against ncurses. Add -lcurses to the gcc command line.
This is exactly what you find in the same book as where you found this code:
$ cc -o badterm badterm.c -lncurses
Beginning linux programming 4th edition, chapter 5: Terminals, page 196.
I am working from a book: TCP/IP Sockets in C and its website code.
I am trying to build a client and server based on those files. My make gives lots of
error related to not being able to find functions from DieWithMessage.c
Here it is:
#include <stdio.h>
#include <stdlib.h>
#include "Practical.h"
void DieWithUserMessage(const char *msg, const char *detail) {
fputs(msg, stderr);
fputs(": ", stderr);
fputs(detail, stderr);
fputc('\n', stderr);
exit(1);
}
void DieWithSystemMessage(const char *msg) {
perror(msg);
exit(1);
}
When I do gcc DieWithMessage.c, I get the following error:
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function _start':
(.text+0x18): undefined reference tomain'
collect2: ld returned 1 exit status
How do I compile this by itself so that the errors will stop happening when using the makefile?
Thanks for any help.
Your C code needs a main function if you're going to try an link/run it. This is a requirement for hosted C applications under the standard.
That error message indicates what's wrong. The C runtime/startup code (CRT) has an entry point of start which sets up the environment then calls your main. Since you haven't provided a main, it complains.
If you only want to generate an object file for later linking with a main (see here for one description of the process), use something like:
gcc -c -o DieWithMessage.o DieWithMessage.c
(-c is the "compile but don't link" flag). You can then link it later with your main program with something like (although there are other options):
gcc -o myProg myProg.c DieWithMessage.o
If you want a placeholder main to update later with a real one, you can add the following to your code:
int main (void) { return 0; }