Error in clock_gettime in ctime - ctime

I have this function:
void Estatisticas::iniciarTempo() {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tempoInicial);
}
I'm trying to use clock_gettime by ctime, but I getting some errors:
"undefined reference to 'clock_gettime'"
Thanks a lot.

From the clock_gettime/clock_getres manpage:
Link with -lrt.
The symbol is defined in the POSIX Real Time library. Add "-lrt" to your compilation line:
gcc -lrt -o myprog myprog.c

Related

How to linck libname.so to C?

I have simple C-code and big libname.so, with libname.h of corse.
I tryed gcc -L. -lurpcadc -o -I Ctest.c, i did preliminarily export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/way/to/lib, but i caught next errors:
/tmp/cc4jgpfg.o: In function `main':
Ctest.c:(.text+0x10): undefined reference to `func_from_lib'
...
collect2: error: ld returned 1 exit status
What i need to do?
It is too simple, but sometimes I have problems with this kind of thing.
gcc Ctest.c -I. -L. -lname -o Ctest
Headings were searched in the wrong place.

clock_gettime() doesn't work

I read the following manual:
http://linux.die.net/man/3/clock_gettime
and I wrote the following code:
#include <time.h>
int main() {
struct timespec clk;
clock_gettime(CLOCK_REALTIME, &clk);
return 0;
}
Surprisingly, I get the following errors:
Symbol CLOCK_REALTIME could not be resolved
undefined reference to clock_gettime
I still don't understand what is the problem. I included the header, and these names show in this header.
maybe you should use#define _POSIX_TIMERS,#define _REENTRANT
besides, when you compile the code, make sure to link the real-time library which is cc filename.c -o filename -lrt
Update 1.0:
sometimes in windows or mac os, C ide may not include real-time library automatically, or we may not used the posix directly without _POSIX_TIMES, therefore you have to link the real-time library manually. In Linux, you can just type in cc filename.c -o filename -lrt to compile the c file.

Undefined reference to a function in another library

I'm trying to compile an object code with a reference to one lib. This is the code of libexample.c:
#include "libexample.h"
#include <signal.h>
#include <time.h>
timer_t sched;
struct itimerspec timer = {{0, 0}, {0, 0}};
void init() {
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sigaction(SIGALRM, &sa, NULL);
timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &sched);
timer_settime(sched, TIMER_ABSTIME, &timer, NULL);
}
And the simple code of a example program:
#include "libexample.h"
int main() {
init();
return 0;
}
I use this to compile:
gcc libexample.c -c -lrt -o libexample.o
gcc example.c -lrt ibexample.o -o example
And I get this when I'm trying to compile with the second line:
./libexample.so: undefined reference to `timer_create'
./libexample.so: undefined reference to `timer_settime'
Anyone knows what I'm doing wrong?
Add -lrt to your link command. timer_create and timer_settime are not part of the C Standard library.
gcc -fPIC -shared libexample.c -lrt -o libexample.so
gcc -L. example.c -lexample -o example
The man timer_create command explains you:
NAME
timer_create - create a POSIX per-process timer
SYNOPSIS
#include <signal.h>
#include <time.h>
int timer_create(clockid_t clockid, struct sigevent *sevp,
timer_t *timerid);
Link with -lrt.
So you should, as documentation says, link with -lrt.
So use
gcc libexample.c -fPIC -shared -o libexample.so -lrt
to produce your libexample.so.
As undur_gongor commented, you need to put the libraries in good order after all the rest (the usual order for gcc arguments is source files, object files, libraries in dependency order) in gcc or ld commands (and that is documented in ld documentation, and in gcc ones). So -lrt should go last.
And learn to read man pages.
Looks like you forgot to link in the library that defines timer_create and timer_settime -- you need to add -lrt to your gcc command.
(source: http://www.kernel.org/doc/man-pages/online/pages/man2/timer_create.2.html)
If you are using cmake, make sure you include the libraries using target_link_libraries(). For e.g., timer functions like timer_create() you need "rt" and for pthread you need "pthread" added using target_link_libraries().

Multithreaded program C using multiple .cpp files

I have a multi threaded program in C that was working well but was in one single main.cpp file.
I have moved the thread in another .cpp file and added it's signature, void* displayScreen(void*); , in the header. I include the header in my initial main.cpp file.
Compiling works but the linker returns an error when trying to call pthread_create(): undefined reference to `displayScreen(void*)'
It looks like it compiled displayScreen(void *) fine but does not know where to load it from. Is there a way for me to tell the linker where to find it or am I doing it wrong please?
Thank you very much.
Adding the signature alone lets you compile the main translation unit, but you still have to compile the implementation of the function separately and link the two:
main.cpp
void* displayScreen(void*);
int main()
{
/* .... */
}
display.cpp
void* displayScreen(void*)
{
/* implementation */
}
Compile:
g++ -O2 -o main.o main.cpp
g++ -O2 -o display.o display.cpp
Link:
g++ -o myprogram main.o display.o -lpthread -s

Compiling C++ program with POSIX AIO lib on Linux

I'm having difficulty with the linker when it comes to compiling a sample program that uses the POSIX aio library (e.g. aio_read(), aio_write(), etc) on Linux.
I'm running Ubuntu with a 2.6 kernel, and have used the apt-get utility to install libaio. But even though I'm linking with the aio library, the compiler still gives me linker errors.
root#ubuntu:/home# g++ -L /usr/lib/libaio.a aio.cc -oaio
/tmp/cc5OE58r.o: In function `main':
aio.cc:(.text+0x156): undefined reference to `aio_read'
aio.cc:(.text+0x17b): undefined reference to `aio_error'
aio.cc:(.text+0x191): undefined reference to `aio_return'
collect2: ld returned 1 exit status
Where are all these aio_x functions actually defined, if not in the library libaio.a?
I also had issues linking against libaio in spite of the aio package being correctly installed and the -lrt flag being present.
It turned out that placing -l flags later (for example, last) in the gcc command invocation sometimes fixes this issue. I stumbled upon this solution here on Stack Overflow.
I stopped doing this:
gcc -Wall -Werror -g -o myExe -lrt myExe.c
And started doing this:
gcc -Wall -Werror -g -o myExe myExe.c -lrt
EDIT: according the the man page, libaio.so is not the correct library to link to:
man aio_read
SYNOPSIS
#include <aio.h>
int aio_read(struct aiocb *aiocbp);
Link with -lrt.
so you should link with this:
g++ -lrt aio.cc -o aio
The way libraries work with gcc is like this:
-L adds directory dir to the list of directories to be searched for -l.
-l adds a library itself, if the file is named libsomename.so, you just use "-lsomename"
Does -L specify the search path and -l specifies the actual library?
You want -laio in order to link to libaio. The argument of -o is what you want the compiled executable to be called.
Try:
sudo apt-get install libaio-dev
Then make sure you specify -laio on the link line.
Okay, Evan Teran is correct - it worked when I linked with -lrt. It seems the aio_x functions are defined in a general POSIX extension library.
Thanks, Evan.

Resources