C - Unable to get shmget/shmat to compile - c

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.

Related

C undefined reference to InetPtonW

I'm trying to use InetPtonW:
if(InetPtonW(AF_INET, argv[1], &ThisSenderInfo.sin_addr)<=0) {
return 1;
}
However I get the following message when compiling:
warning: implicit declaration of function 'InetPtonW' [-Wimplicit-function-declaration]
undefined reference to `InetPtonW'
collect2.exe: error: ld returned 1 exit status
I've read the documentation located here and I've followed everything but still can't get it to work.
• I'm compiling with Ws2_32 library gcc test.c -o test -lws2_32 using MinGW
• I've included the needed header files #include <ws2tcpip.h> and #include <windows.h>
• I've tried using InetPton but it returns the same error
• Running on Windows 10
I recall running into this exact issue some many months ago. #alk's comment points to a question whose accepted answer feels very similar to what fixed it for me.
You should be able to #define a version macro (or two) before your #include lines to fix it.
While I feel strongly that the aforementioned answer is correct, I'll update this answer later today when I can verify.
Update!
The code I was referencing above doesn't have InetPtonW in it anymore but it had the necessary #defines in it. Here's a brief example that compiles on my machine (win10/mingw64/gcc 8.2.0):
Z:\Some\Directory>gcc test.c -o test -lmswsock -lws2_32
#define NTDDI_VERSION NTDDI_VISTA
#define WINVER _WIN32_WINNT_VISTA
#define _WIN32_WINNT _WIN32_WINNT_VISTA
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
/* This is "test.c", please pardon the lack of error checking. */
int main(void) {
BYTE ipbuf[4] = {0};
WSADATA wsa;
WSAStartup(MAKEWORD(2,2), &wsa);
printf("%d: ", InetPtonW(AF_INET, L"127.0.0.1", &ipbuf));
for(int i = 0; i < 4; ++i)
printf("%hhu.", ipbuf[i]);
WSACleanup();
}
Output should look like:
Z:\Some\Directory>gcc test.c -o test -lmswsock -lws2_32
Z:\Some\Directory>test
1: 127.0.0.1.
Z:\Some\Directory>
It's a linking error. which say that, included library path, the given function not found. please make sure your dll library path for InetPtonW or make sure that is available in your system or not.

Unable to compile a C file in Contiki

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.

clone system call OS X not linking - undefined symbols [duplicate]

This question already has answers here:
Where is clone() method in sched.h on Mac OS X
(2 answers)
Closed 9 years ago.
I would like to use the clone system call on OS X. It's a Unix system call so it shouldn't be a problem, right? I have successfully tried using fork, vfork and other similar functions. Here is the program I'm trying:
#include <sched.h> //Clone resides here
#include <stdlib.h> //standard library
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <sys/shm.h>
#include <errno.h>
#include <sys/sem.h>
#include <signal.h>
#include <sys/types.h>
int helloWorld();
int main(int argc, char *argv[])
{
int (*functionPointer)() = &helloWorld; //The first argument of clone accepts a pointer to a function which must return int
void **childStack = (void**)malloc(1024); //We will give our child 1kB of stack space
clone(functionPointer, childStack, 0, NULL); //First arugment is the function to be called, second one is our stack, CLONE_VM means to share memory, last NULL PID description
return 0;
}
int helloWorld()
{
printf("Hello (clone) world!\r\n");
return 0;
}
Compiling with gcc -o test my_file.c gives:
Undefined symbols for architecture x86_64:
"_clone", referenced from:
_main in ccG3qOjx.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Feel free to ignore the comments, since I'm just learning. And one more thing.. if I try to pass CLONE_VM in the arguments it doesn't even compile giving me the error:
my_file.c: In function ‘main’:
my_file.c:12: error: ‘CLONE_VM’ undeclared (first use in this function)
my_file.c:12: error: (Each undeclared identifier is reported only once
my_file.c:12: error: for each function it appears in.)
Am I missing an #include? If so, which one?
What am I doing wrong and how to fix it?
clone is specific to Linux, so for OS X you're stuck with fork, or use threads if you can manage with that.

How do I use libdb-4.2 in a FreeBSD 9.1 system?

I'm attempting to write a small program in C that will open and read from a Berkeley 4.2 hash DB on a FreeBSD 9.1 system for testing, but I can't get it to compile. This is the first time I've written anything in C and compiled from a command line so I'm probably missing one thing that'll get it working, I don't know.
After searching all over and looking at documentation and source code on github, this is what I've got so far:
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <db.h>
int main()
{
DB * mydb;
u_int32_t open_flags = DB_RDONLY;
int ret;
ret = db_create(&mydb, NULL, 0);
if (ret != 0) {
printf("Error creating DB structure!");
return 1;
}
ret = mydb->open(mydb, NULL, "bsddb-py", NULL, DB_HASH, open_flags, 0);
if (ret != 0) {
printf("Error opening DB file!");
return 2;
}
mydb->close(mydb, 0);
}
I compile with this:
cc -ldb-4.2 db_test.c
And get this:
db_test.c: In function 'main':
db_test.c:20: error: 'DB_RDONLY' undeclared (first use in this function)
db_test.c:20: error: (Each undeclared identifier is reported only once
db_test.c:20: error: for each function it appears in.)
db_test.c:29: error: 'DB' has no member named 'open'
db_test.c:35: error: too many arguments to function 'mydb->close'
Apparently the compiler is hung up on using Berkeley 1.85 (dbopen and such) and it won't budge?
It looks like #include <db.h> will provide an interface to Berkeley 1.85 because that's what's installed by default on FreeBSD. We have Berkeley 4.2 installed via ports, and to avoid conflicts, the header that interfaces with 4.2 is put elsewhere - I was referencing the right library but not the right header.
So, I changed the include to:
#include <db42/db.h>
...and compiled with...
cc -I/usr/local/include/ -L/usr/local/lib/ -ldb-4.2 db_test.c -o db_test
Running the above source with that modification produced no visible output, which means it worked!
As a newbie to it, BSD is weird.
DB_RDONLY is contained in some header file that you are not #including. That should take care of all the line 20 errors.
Line 29: a DB struct apparently doesn't have a member named open. Recheck the struct/maybe you forgot to include the file that that struct is declared in.
35: Seems like the function close doesn't take 2 arguments. Recheck this in the header file/make sure you included the header file.

Compile Attempt Gives crt1.o/'start'/undefined reference to 'main'/exit status message

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; }

Resources