test1.c
#include <stdio.h>
int main(void) {
printf("test\n");
delay(1000);
printf("test2\n");
}
When I try to compile...
gcc test1.c -o test1
Undefined symbols for architecture x86_64:
"_delay", referenced from:
_main in ccUnw3tY.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Certainly there is a lesson here in knowing your libraries and what linking is etc... What am I missing? I am trying to do this on OSX.
There's no delay function in C, you have to use sleep or usleep depending on what OS you're on.
What make you think there is a delay function. I dont see one in the osx docs. There is a sleep function
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/sleep.3.html
An alternative of delay in C for unix os is the sleep function :
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/sleep.3.html
do something like :
#include <stdio.h>
#include <unistd.h>
int main(void) {
printf("test\n");
usleep(1000);
printf("test2\n");
}
If you value is for 1000 microsecondes.
The delay function works in Borland C compiler. You have to use the dos.h header file in order to use delay. Some other compilers like MinGW may not support this.
Related
Code:
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/wait.h>
#define _GNU_SOURCE
void *stack_memory()
{
const int stackSize = 65536;
void* stack = (void*)malloc(stackSize);
if (stack == NULL) {
printf("%s\n", "Cannot allocate memory \n");
exit(EXIT_FAILURE);
}
return stack;
}
int jail(void *args)
{
printf("Hello !! - child \n");
return EXIT_SUCCESS;
}
int main()
{
printf("%s\n", "Hello, world! - parent");
clone(jail, stack_memory(), SIGCHLD, 0);
return EXIT_SUCCESS;
}
Error:
Undefined symbols for architecture x86_64: "_clone", referenced
from:
_main in docker-4f3ae8.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Linux doesn't prefix symbols with a leading _ so you're not using Linux.
But the clone(2) system call is Linux-specific, according to its man page.
clone() is Linux-specific and should not be used in programs intended
to be portable.
Probably you're using OS X or something. And you're compiling as C, so calling an un-declared function isn't a compile-time error (just a big warning). This is why it's a linker error instead of a compile-time error (and you ignored compiler warnings.)
And BTW, #define _GNU_SOURCE after including header files is pointless. You have to define feature-request macros before including headers to get them to define prototypes for GNU-only functions in cases where that's not already the default.
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.
say I have a parent and a child, the child calls a function "hello" in the child with dlopen. Can the child then call a function "world" in the parent? I keep getting symbol lookup error: ./child.so: undefined symbol: world
here are the files. parent.c
#include <dlfcn.h>
typedef void (*fptr)();
#include <stdio.h>
int main () {
void*handle=dlopen("./child.so",RTLD_LAZY);
fptr f=dlsym(handle,"hello");
f();
return 0;
}
void world() {
printf ("world");
}
and child.c
#include <stdio.h>
void hello () {
printf ("hello");
world();
}
Yes, it a dlopen-ed module can call functions from the calling program, provided that the calling program has been linked with the -rdynamic option.
BTW, most plugins need that feature: a firefox plugin obviously wants to call firefox functions.
Read also about visibility function __attribute__ ... Read also Drepper's How to Write Shared Libraries long paper and dlopen(3) man page.
I found the answer on google
http://www.justskins.com/forums/dlopen-calling-functions-other-104185.html
gcc -rdynamic hello.c -ldl
Surprisingly it can. I've personally seen it happen.
You may need to link to the executable with -rdynamic
I'm getting some problems on compiling a very very simple name.c file on Mac OSX Lion.
Now, I started following Harvard CS50 course on cs50.net. I'm not totally new to programming but I was curious on how this course has been taught.
This is the source of name.c:
#include <stdio.h>
#include <cs50.h>
int
main(void)
{
printf("State your name:\n");
string name = GetString();
printf("O hai, %s!\n", name);
return 0;
}
As you can see, it requires this library: https://manual.cs50.net/CS50_Library.
Now, when I compile it, this happens:
Undefined symbols for architecture x86_64:
"_GetString", referenced from:
_main in name-vAxcar.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [name] Error 1
If I use the same GetString() cs50.c function inside my source file, it works perfectly:
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
typedef char *string;
string GetString(void);
int
main(void)
{
printf("State your name:\n");
string name = GetString();
printf("O hai, %s!\n", name);
}
string
GetString(void)
{
// CODE
}
Why does this happen?
I installed the library as it says on the link above; I checked and both cs50.h and libcs50.a are respectively in /usr/local/include and /usr/local/lib.
Thank you in advance for your help.
The problem you encounter is in the linking stage, not compiling. You did not provide the implementation of GetString, only its declaration (through the .h file you #include).
To provide the implementation itself, you usually need to link against the library which includes it; this is usually done by the -l flag to g++. For example,
g++ file.cpp -lcs50
Your second sample code does link, because you manually (and explicitly) provide an implementation for GetString, though an empty one.
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.