Shared Memory Library -lrt in C on macOS - c

I'm doing a university assignment on shared memory on C, I need to use the functions shm_open(), mmap(), ftruncate(), shm_unlink(), etc. using a Mac with macOS Mojave 10.14
My teacher told me I need to use the -lrt library in my Makefile to make it work but when I "make" I got an error which goes
ld: library not found for -lrt
clang: error: linker command failed with exit code 1
Is the library spelled work? If I don't have it, I need to download it? Do I even need a specific library? ('cause on the internet I didn't find anybody using one...)
I'm very confused.

Related

What is the C library "rt"? [duplicate]

i'm getting some troubles with QT it builds with option "-lrt"
i'm using mac os 10.6 with QT creator 1.2.1, heeeeeeelp !
this is the full build command :
g++ -headerpad_max_install_names -o
AMiningCoreTest main.o tokenizer.o
DictionnaryToolBox.o mysql.o btree.o
BTreeDataTable.o tcaccess.o
-L/Library/Frameworks -L/usr/lib/mysql -lmysqlclient -L/usr/local/lib/ -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc
and it ends with
ld: library not found for -lrt
collect2: ld returned 1 exit status
The linker cannot find librt which is probably the Posix real time extensions library. I don't think this is available on OSX. Googling gives this from Apple developer lists
Question from list
I'm trying to build a simulator developed in my university (on Linux)
and I get error by the linker that seems unable to find librt.a - in
the code is used for clock_gettime() and I would like to know if
there's a port of such library, or some other similar function that
allows me to compile even on Mac OS X.
Answer
librt.a is the System V name of the library containing the POSIX
Advanced Realtime [RT} Option functions. The specific function you are
asking about is part of the [TMR] option. If Mac OS X supported it, it
would be in libSystem.B,dylib, not librt.a. The function in question
is not supported by Mac OS X.
Your code should check to see whether optional to implement things
above and beyond the UNIX standard are implemented in the target OS,
and if they aren't, use a different interface.
Why does the program need librt?
I know that some platforms (Solaris comes to mind) require librt for some functions which might exist in other libraries in your OS. (sem_init() et al. are like this)
You might try to link without -lrt and see if it works.

CBLAS mac OS X Undefined symbols for architecture x86_64 error

I'm trying different C linear algebra libraries for my projects and now I want to learn BLAS (CBLAS). I am trying to follow the tutorial here. I realised that cblas is already built in the xcode and by adding the flag
gcc foo.c -framework Accelerate
or
gcc foo.c -lcblas
I can remove most of the errors I had before. however there is a final error which I can not find anywhere on the internet.
Undefined symbols for architecture x86_64:
"_printVector", referenced from:
_main in blas1C-63e43d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm not sure where I'm making the mistake but there are a number of possibilities:
I need to instal BLAS/CBLAS from the netlib website. I actually tried to do this but I could manage to build the library from the source code! it would be great if somebody could make a Homebrew formula. then if the original CBLAS library installed then I need to learn about the gcc flags for compiling and how to link the libraries.
or there are syntax differences between the original CBLAS from netlib and the one built in the xcode and I need to change the code.
thanks for your help in advance.
P.S. I'm trying to compile the codes blas1C.c, blaio.c, blaio.h from the page I mentioned.
edit 1: oh my! I just realised that I have made a horrible mistake. the printVector function missing is not part of CBLAS but a function made by the author of the blog. the only thing I had to do was just to compile the blaio.c file as well. so the correct gcc command should be
gcc blas1C.c blaio.c -lcblas
or
gcc blac1C.c blaio.c -framework Accelerate
That function comes from the blasio.c in the website and declared in blasio.h
You need to build blasio and link to it

How to get linker to report where it is linking to for a given function

I have an issue trying to cross build win32 and win64 exes on a linux host.
I am using the mingw cross build toolchains
my .c file includes time.h in order to use clock_gettime() in main()
now this is a POSIX thing so no guarantee it is windows portable
however, on another laptop with a similar (but obviously not identical) setup it does compile and link no problem
on this laptop (a new one I am migrating to) I get a linker error:
undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status
what I would like to be able to do is somehow have the linker on the other machine tell me where it is finding the .dll with clock_gettime() in it
In order for me to see whether the similar .dll is present on the new laptop and whether the clock_gettime() symbol is avaiable in it
Is it possible to get the linker to report this info, some sort of verbose mode perhaps. I've gone down the GIYF route but drawn a blank thus far.
Compile with -lrt which is needed for for glibc version < 2.17.
What probably happens on the other laptop is that it has a recent version of glibc >=2.17 in which the the clock_gettime() is part of the libc. But older in glibcs, it's a separate library. Hence, you needed to link it yourself.
To use clock_gettime() as defined in <time.h> when cross building for windows using mingw toolchain on a linux host you must link to pthread not rt
for example:
source code, example.c, looks like this:
#include <time.h>
...
struct timespec t1;
...
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);
native build looks like this:
gcc example.o -lrt -o example
win32 cross-build looks like this:
i686-w64-mingw32-gcc -I/usr/local/i686-w64-mingw32/include example.o -L/usr/local/i686-w64-mingw32/bin -lpthread -lws2_32 -o example.exe
Unfortunately, I am none the wiser on how to get the linker to tell me in which library it has found a function that it has successfully linked to
i.e. if I could have somehow got the linker to inform me that it had found clock_gettime() in libpthread on my other machine that was successfully linking, I could have saved a lot of messing about fixing the link error issue on this machine.

Compile shared library with link to other .so

I want to link an existing shared library (FlashRuntimeExtensions.so) to my C-code while compiling my own shared library. But whatever I try I always get the same error; that the file is in a wrong format. Does anybody have an idea on how to solve this?
Here is my compile command:
$ g++ -Wall ane.c FlashRuntimeExtensions.so -o aneObject
FlashRuntimeExtensions.so: could not read symbols: File in wrong format
collect2: ld gaf exit-status 1 terug
Your command line tries to generate x86 code and link it to ARM code using the native g++ available in your distribution.
This will not work. Use the Android NDK available here: http://developer.android.com/tools/sdk/ndk/index.html
The NDK includes a set of cross-toolchains (compilers, linkers, etc..) that can generate native ARM binaries on Linux, OS X, and Windows (with Cygwin) platforms.
In general .so will be linked using -l.
for example, pthread -lpthread we use.
gcc sample.c -o myoutput -lpthread
But as per #chill's statement, what you are doing in command is correct only.
I suggest you to refer the following link.
C++ Linker Error SDL Image - could not read symbols
It should be an architecture mismatch. I faced this problem once, I have solved it by building the libs in same target platform and it is obvious. If you are using linux or Unix like OS you can see that by file command and if you are using windows you can see that using Dependency Walker. You need to make sure that all the libs matches architecture.

library not found for -lrt with QtCreator [mac os]

i'm getting some troubles with QT it builds with option "-lrt"
i'm using mac os 10.6 with QT creator 1.2.1, heeeeeeelp !
this is the full build command :
g++ -headerpad_max_install_names -o
AMiningCoreTest main.o tokenizer.o
DictionnaryToolBox.o mysql.o btree.o
BTreeDataTable.o tcaccess.o
-L/Library/Frameworks -L/usr/lib/mysql -lmysqlclient -L/usr/local/lib/ -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc
and it ends with
ld: library not found for -lrt
collect2: ld returned 1 exit status
The linker cannot find librt which is probably the Posix real time extensions library. I don't think this is available on OSX. Googling gives this from Apple developer lists
Question from list
I'm trying to build a simulator developed in my university (on Linux)
and I get error by the linker that seems unable to find librt.a - in
the code is used for clock_gettime() and I would like to know if
there's a port of such library, or some other similar function that
allows me to compile even on Mac OS X.
Answer
librt.a is the System V name of the library containing the POSIX
Advanced Realtime [RT} Option functions. The specific function you are
asking about is part of the [TMR] option. If Mac OS X supported it, it
would be in libSystem.B,dylib, not librt.a. The function in question
is not supported by Mac OS X.
Your code should check to see whether optional to implement things
above and beyond the UNIX standard are implemented in the target OS,
and if they aren't, use a different interface.
Why does the program need librt?
I know that some platforms (Solaris comes to mind) require librt for some functions which might exist in other libraries in your OS. (sem_init() et al. are like this)
You might try to link without -lrt and see if it works.

Resources