Keep getting semaphore compile problem can someone help me? - semaphore

Im trying to compile program with semaphores but i keep getting errors like this:
https://i.stack.imgur.com/3pQMI.png
Does anyone know how to solve this?

The answer was:
gcc <inputfile.c> -o <outputfile> -lpthread -lrt

Related

Compile static library using g++

I have 3 classes - I denote those by firstClass,secondClass,thirdClass.
My headers - firstClass.h, secondClass.h, thirdClass.h and sources firstClass.cpp, secondClass.cpp, thirdClass.cpp.
In class thirdClass I create instance of firstClass and two instance of secondClass.
In main.cpp I deamonize and create instance thirdClass.
I want to create static library of thirdclass and linking to main.cpp.
firstClass and thirdClass used the same library libm.a
I created library step by step as following:
g++ -c -I-/usr/include/ -I-/usr/lib/ -I-/home/projects/Learninig firstClass.cpp -lstdc++ -lm-o WsChannel.o -w -m32
g++ -c -I-/usr/include/ -I-/usr/lib/ --I-/home/projects/Learninig secondClass -lstdc++ -o secondClass.o -w -m32
g++ -c -I-/usr/include/ -I-/usr/lib/ --I-/home/projects/Learninig thirdClass.cpp -lstdc++ -lm -o thirdClass.o -w -m32
ar rcs libLearning.a firstClass.o secondClass.o thirdClass.o
g++ main.cpp -L. -lLearning -lm -o MnLearning.o -m32
Compiling was maked correctly without any errors, but when I execute program I have same error. I spent some hours on checking code, but I don't find bugs. So then maybe compiling was incorrect. I did this using some tutorial in web.
If whatever was unclearly I am ready to more explain my question.
Edit: My error:
segfault at 557400000045 ip 00005574bd509dcd sp 00007ffd9e887900 error 4 in MnLearning[5574bd4f2000+26000]
The error is surely inside your own source code. Avoid undefined behavior in it, and be scared of UB.
Your use of -I- is strange, and probably wrong. I recommend removing it (and also, at first, remove the -m32 flag if your computer and distribution is 64 bits; work first to have your program run correctly on your laptop, then port it later to 32 bits Linux by adding the -m32 flag). You might use preprocessor options like -H to be shown what files are included.
I recommend building your library and your program with some build automation tool, such as GNU make or ninja.
Configure your build to compile with all warnings and debug info, i.e. using g++ -Wall -Wextra -g with GCC. Improve your source code to get no warnings. Then use the gdb debugger to understand the behavior of your program (and library).
So then maybe compiling was incorrect.
No, the compiler is probably good, and you should trust it.
The bug is very likely to be in your own code.
My error: segfault at 557400000045 ip 00005574bd509dcd sp 00007ffd9e887900 error 4 in MnLearning[5574bd4f2000+26000]
Segmentation fault is a symptom of some error in your own code (e.g. some buffer overflow, some bad pointer dereference, etc; or other kind of UB).
You might also use valgrind.
I spent some hours on checking code, but I don't find bugs.
You did not spend enough time (some bugs may take you weeks of work to be found), and you forgot to use the debugger, a very handy tool to help you understand the behavior of your program and find bugs in it. Be aware that programming is difficult, and don't be discouraged.

undefined reference to mbind

I did upgrade my Ubuntu from 11.04 to 11.10. In 11.04 all this worked fine:
gcc -fopenmp -g -c -o test.o test.c -I../include
gcc -fopenmp -g -o test test.o -lnuma -lm -L../myLib -lmyL
but now, in 11.10, it gives the following errors:
../myLib/libmyL.a (get.o): In function 'get':
get.c:(.text+0xa5): undefined reference to `mbind'
Where mbind is http://www.kernel.org/doc/man-pages/online/pages/man2/mbind.2.html
Plz can you help me to solve this problem? Or at least tell me what went wrong? Thanks.
Move -lnuma to after your library in the link line this will help solve the dependency - because when you have it on the link line first the linker drops it because it doesn't need it as far as it knows so far.
Oh and in answer to your possible follow up question, yes you can create circular dependencies and No the they are not fun to fix :-)

gcc:linker input file unused because linking not done

When I run a make file in Linux to compile C codes, I get the following error:
gcc -Wall -fPIC -DSOLARIS -DXP_UNIX -DMCC_HTTPD -D_REENTRANT -I/opt/profile/OraAlert_test/code/include -I/usr/netscape/server4/plugins/include -I../../pwutils -I../../database/src -I../../access/src -I/data/share/capscan/include -o getEnv.o -c ../src/
gcc: ../src/: linker input file unused because linking not done
I have tried searching for related questions in stackoverflow and tried the the solutions suggested. Still this could not be resolved.
Any suggestions?
You have a compilation command without a source file.
What is it supposed to compile?
The error is indeed misleading. It assumes you want to link with ../src/, but -c says no linkage is to be done.

std.net.curl linker errors in Linux

I'm trying to use std.net.curl on 32-bit Ubuntu Linux (11.10 Ocelot) using DMD 2.058.
When I build (dmd source.d) I get:
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl19_sharedStaticCtor30FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticCtor30FZv+0x6): undefined reference to `curl_global_init'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl19_sharedStaticDtor31FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticDtor31FZv+0x4): undefined reference to `curl_global_cleanup'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl_12fd_140.o): In function `_D3std3net4curl4HTTP4Impl6__dtorMFZv':
std/net/curl.d:(.text._D3std3net4curl4HTTP4Impl6__dtorMFZv+0x12): undefined reference to `curl_slist_free_all'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl_12fd_140.o): In function `_D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP':
std/net/curl.d:(.text._D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP+0x53): undefined reference to `curl_slist_append'
[snip]
I added -L-lcurl to my command line (full command: dmd source.d -L-lcurl) but I get the exact same result. I have libcurl4-openssl installed. I can build a simple C curl program on this computer without issue (gcc simple.c -lcurl). I'm not sure where to look from here to figure this out.
Update: Here is how the linker is invoked according to dmd -v:
gcc source.o -o source -m32 -lcurl -Xlinker -L/usr/lib -Xlinker -L/usr/lib64 -Xlinker --no-warn-search-mismatch -Xlinker --export-dynamic -lphobos2 -lpthread -lm -lrt
As you yourself discovered, the order of libraries is incorrect. A quote from "An Introduction to GCC": "A library which calls an external function defined in another library should appear before the library containing the function." In your case libphobos calls external library's (libcurl) function(s), so I guess libphobos should go before libcurl.
The book is available online. Chapter related to the OT is here: http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html
The DMD compiler is clearly broken in this respect. I published a patch to fix it (maybe not the best) but the maintainer of the compiler is not convinced this needs to be fixed at all so there is no much discussion about it.
https://github.com/D-Programming-Language/dmd/pull/497
http://d.puremagic.com/issues/show_bug.cgi?id=7044
Seemingly the simplest way to fix the phobos2/curl linker order problem, as mentioned elsewhere in this thread:
dmd source.d -L-lphobos2 -L-lcurl
Works like a charm for me.
I figured out if I obtain the linker command through -v and reorder it so the -lcurl is at the end it links without issue.
gcc source.o -o source -m32 -Xlinker -L/usr/lib -Xlinker -L/usr/lib64 -Xlinker --no-warn-search-mismatch -Xlinker --export-dynamic -lphobos2 -lpthread -lm -lrt -lcurl
I'm still curious if there is a way to fix this without manually running the linker.

Error while loading shared library: libconfig++.so.9

While I compile the code in Makefile, I've put LINKPATH = -L/usr/local/lib in the g++ line. And of course "libconfig++.so.9" is under /usr/local/lib
But when I execute the executable file, it still says cannot find libconfig++.so.9.
Anyone knows why?? Appreciate any help.
You should define LD_LIBRARY_PATH.
For example:
export LD_LIBRARY_PATH=/usr/local/lib
or just use
sudo ldconfig -v

Resources