Getting undeclared function and undefined symbol errors with rand_r when compiling a multithread C program on Windows - c

I am creating many threads and each one should output a random number.
I know that srand() with rand is not thread-safe and indeed all the output numbers are the same.
So I tried to use rand_r but I get the following error on my Windows terminal
main.c:47:16: warning: implicit declaration of function 'rand_r'; did you mean 'rand'? [-Wimplicit-function-declaration]
result= ( (rand_r(&seed) % (high+1-low) ) + low);
^~~~~~
rand
main.c: In function 'customerServe':
main.c:333:1: warning: control reaches end of non-void function [-Wreturn-type]
}
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe:
C:\Users\allys\AppData\Local\Temp\ccSslADA.o:main.c:(.text+0xe): undefined
reference to `rand_r'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe:
C:\Users\allys\AppData\Local\Temp\ccSslADA.o:main.c:(.text+0x41c):
undefined reference to `rand_r'
collect2.exe: error: ld returned 1 exit status
Thank you

I saw from the post tags that you are using the "pthreads" library which stands for POSIX threads. Therefore this project cannot be run on Windows since it does not support the "lpthread" flag on your system.
If you insist on working on a Windows machine, you could use something like this which lets the developer work on an Ubuntu terminal from windows. While having access to an Ubuntu-like system where the lpthreads library is supported, you can move on with your project. Another possible solution could be using docker to compile & run your project on an isolated ubuntu environment but this is kind of an overkill.
Let me know if this helped!

I needed to specify a late enough Posix source to avoid this warning, e.g.:
gcc -Wall -std=c11 -D_POSIX_C_SOURCE=199506L pi.c -o pi -lm -lpthread

Related

make error using GNU/GCC quad precision library

When using Cygwin and GCC under Win 7 and the quad precision library:
gcc -c lquad.c
... this runs OK, but
gcc lquad.o
... produces the following error:
/tmp/ccLM2lOn.o:lquad.c:(.text+0xdb3): undefined reference to `sqrtq'
collect2: error: ld returned 1 exit status
I have #include quadmath.h in a C source, and the __float128 type works, but not the functions (e.g. sqrtq above). This header file has all the relevant externs, e.g.:
extern __float128 sqrtq (__float128) __quadmath_throw;
I found a dll cygquadmath-0.dll which definitely has these functions in it. It is in 3 places in the Cygwin installation. Additionally, I copied it to Windows\System, but that did not help.
There is a related question here, but nothing helped me there and it is not exactly the same error: Quadruple Precision in C++ (GCC)
You are looking for a compiler solution when what you need is a linker solution:
Your code is compiling as it should, so no change in the code would solve your problem:
/tmp/ccLM2lOn.o:lquad.c:(.text+0xdb3): undefined reference to `sqrtq'
collect2: error: ld returned 1 exit status
The undefined reference to errors indicate a function needed by your object is in another library.
As suggested by #Marc Glisse, try adding -lquadmath to your linker line.

Using exp101 and log101 functions in C

I am trying to use the function exp1 and log101 which take long doubles as input and output long doubles in turn. However, when I try to compile the file I get the following error message:
/tmp/ccIvBVbG.o: In function `main':
derivative.c:(.text+0x250): undefined reference to `log101'
collect2: ld returned 1 exit status
I have included math.h library and also provided the -lm option. What is the remedy to this issue? Any help and comments are appreciated.
Thanks
P.S. Compiler is GCC on 32-bit Ubuntu 12.04 LTS
The names of the functions you are trying to use are exp10l and log10l. The last character in the name is a lowercase L, not another 1.

Problems using graphviz as a library

I'm trying to use graphviz as a library for a C++ project, following the libguide provided here. However I'm having problems even compiling the examples in the appendix. When I try to compile demo.c using gcc I get the following output:
$ gcc -I/usr/local/Cellar/graphviz/2.28.0/include/ demo.c -L/usr/local/Cellar/graphviz/2.28.0/lib/ -lgvc -lgraph -lcdt
demo.c: In function ‘main’:
demo.c:14: error: ‘Agdirected’ undeclared (first use in this function)
demo.c:14: error: (Each undeclared identifier is reported only once
demo.c:14: error: for each function it appears in.)
demo.c:15: error: too many arguments to function ‘agnode’
demo.c:16: error: too many arguments to function ‘agnode’
demo.c:17: error: too many arguments to function ‘agedge’
Agdirected is found in cgraph.h, but if I change the includes in demo.c to
#include <graphviz/gvc.h>
#include <graphviz/cgraph.h>
Then all hell breaks loose (mostly conflicting declarations between the two headers). How can I include the necessary headers without the headache of all these conflicts?
Mac OS X 10.8.3, Graphviz 2.28.0, GCC 4.2.1
It seems after some experimentation that adding the flag
#define WITH_CGRAPH
has the effect of including cgraph.h, which gets rid of the "'Agdirected' undeclared" error.
The other errors can be fixed by changing the command line option in gcc from -lgraph to -lcgraph
The libguide you are using is the cgraph version, which assumes Graphviz 2.30 or later. With that version, the #define WITH_CGRAPH is already provided.

ld error while building GD library for Haskell on Windows

When installing the Haskell GD package through cabal, on Windows (using MinGW), I get the following warnings:
Warning: resolving _gdImagePtrDestroyIfNotNull by linking to _gdImagePtrDestroyIfNotNull#4
Warning: resolving _gdImageCopyRotated90 by linking to _gdImageCopyRotated#36
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups
Note that these are precisely the functions defined in gd-extras.
Then, when actually compiling a Haskell program which uses gd, I get the following errors:
Linking Main.exe ...
[...]\cabal\gd-3000.7.3\ghc-7.4.1/libHSgd-3000.7.3.a(Internal.o):fake:(.text+0x2211):undefined reference to 'gdImageCopyRotated90'
[...]\cabal\gd-3000.7.3\ghc-7.4.1/libHSgd-3000.7.3.a(Internal.o):fake:(.text+0x500a):undefined reference to 'gdImagePtrDestroyIfNotNull'
[...]
collect2: ld returned 1 exit status
I'm unable to figure out how to fix this — it's already taken me ages to get to this point, as I've had many more issues trying to get it working, but this seems like the final hurdle. I have tried enabling/disabling stdcall fixup, and also changing in which file these functions are defined (as gd-extras seemed to be a potential issue), but that hasn't adressed the issues.
Thanks for any help.
You need to pass explicit linker flags to ghc, pointing to the library. The Haskell GD library is automatically linked, but the dll will not be linked as well unless you tell ghc about it. The stdcall-fixup errors are a red herring here.

How to initialize SNMP in C

Can anyone tell me how to initialize a SNMP agent in c using SNMP library?
The call to "init_agent()" is not working.
I read somewhere that "init_agent()" function should be used before "init_snmp()".
Here is what I am getting while compiling the code:
/tmp/ccEiSj2l.o: In function `main':
agent1.c:(.text+0x95): undefined reference to `init_agent'
collect2: ld returned 1 exit status
I have linked the compiler to snmp library using "-lsnmp".
Did you try passing required libraries to your compiler? Try passing output of the script net-snmp-config --libs . (Put back quotes surrounding that command).
I am used to using Net-SNMP for these things, and if you are attempting to create an agent with Net-SNMP, then you will need to link against the agent library (in addition to whatever other libraries you may be using):
-lnetsnmpagent

Resources