gmp strange behaviour that doesn't let me compile new project - c

I've installed on my ubuntu gmp using this command:
sudo apt-get install libgmp3-dev
and it worked fine.
Now I'm trying to create a new project put simply writing
#include "gmp.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(){
mpz_t num;
mpz_init(num);
printf("%s\n",mpz_get_str (NULL, 10, num));
mpz_clear(num);
return 0;
}
give me
> gcc -lgmp mil.c /tmp/ccHvV9kT.o: In function `main':
> mil.c:(.text+0x1f): undefined reference to `__gmpz_init'
> mil.c:(.text+0x35): undefined reference to `__gmpz_get_str'
> mil.c:(.text+0x49): undefined reference to `__gmpz_clear' collect2:
> error: ld returned 1 exit status
I just copy-pasted the code of my previous project and I get the same error(in all the function that I created), but compiling my old project I don't get any error.
What is my problem???

Order of arguments to gcc matters a big lot.
Try to use (you want warnings and debug info, so)
gcc -Wall -Wextra -g mil.c -lgmp -o milprog
Then run ./milprog. You may want to use the gdb debugger on it, with
gdb ./milprog
and you may want (for benchmarking purposes) to ask the compiler to optimize, by adding (before -g) something like -O2 -march=native
Learn to use GNU make (or some other build automation tool, like ninja), see this.
Be sure to use a version control system like git.
BTW, I find more logical and more elegant to include "gmp.h" after (not before, as you did) the inclusion of standard headers (like <stdio.h>).

Related

Bash script error "undefined reference to sin" [duplicate]

I have this simple code:
max = (int) sqrt (number);
and in the header I have:
#include <math.h>
But application still says undefined reference to sqrt. Do you see any problem here? It looks like everything should be okay.
You may find that you have to link with the math libraries on whatever system you're using, something like:
gcc -o myprog myprog.c -L/path/to/libs -lm
^^^ - this bit here.
Including headers lets a compiler know about function declarations but it does not necessarily automatically link to the code required to perform that function.
Failing that, you'll need to show us your code, your compile command and the platform you're running on (operating system, compiler, etc).
The following code compiles and links fine:
#include <math.h>
int main (void) {
int max = sqrt (9);
return 0;
}
Just be aware that some compilation systems depend on the order in which libraries are given on the command line. By that, I mean they may process the libraries in sequence and only use them to satisfy unresolved symbols at that point in the sequence.
So, for example, given the commands:
gcc -o plugh plugh.o -lxyzzy
gcc -o plugh -lxyzzy plugh.o
and plugh.o requires something from the xyzzy library, the second may not work as you expect. At the point where you list the library, there are no unresolved symbols to satisfy.
And when the unresolved symbols from plugh.o do appear, it's too late.
I suppose you have imported math.h with #include <math.h>
So the only other reason I can see is a missing linking information. You must link your code with the -lm option.
If you're simply trying to compile one file with gcc, just add -lm to your command line, otherwise, give some informations about your building process.
Just adding the #include <math.h> in c source file and -lm in Makefile at the end will work for me.
gcc -pthread -o p3 p3.c -lm
Here are my observation, firstly you need to include the header math.h as sqrt() function declared in math.h header file. For e.g
#include <math.h>
secondly, if you read manual page of sqrt you will notice this line Link with -lm.
#include <math.h> /* header file you need to include */
double sqrt(double x); /* prototype of sqrt() function */
Link with -lm. /* Library linking instruction */
But application still says undefined reference to sqrt. Do you see any
problem here?
Compiler error is correct as you haven't linked your program with library lm & linker is unable to find reference of sqrt(), you need to link it explicitly. For e.g
gcc -Wall -Wextra -Werror -pedantic test.c -lm
I had the same issue, but I simply solved it by adding -lm after the command that runs my code.
Example.
gcc code.c -lm

Compilation error running C code using GSL on Windows to be called through R

I need to call the following C code through R using GSL (GNU lib). The problem I'm having is in the compilation of the C code.
Here is the program mean.c:
#include <stdio.h>
#include <R.h>
#include <math.h>
#include <gsl/gsl_statistics.h>
void gsl(double *x, int *n)
{
double mean = gsl_stats_mean(x, 1, n);
Rprintf("mean = %f\n", mean);
}
Command> R CMD SHLIB mean.c
Output>
cygwin warning:
MS-DOS style path detected: C:/PROGRA~1/R/R-31~1.2/etc/i386/Makeconf
Preferred POSIX equivalent is: /cygdrive/c/PROGRA~1/R/R-31~1.2/etc/i386/Makeconf
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
gcc -m32 -shared -s -static-libgcc -o mean.dll tmp.def mean.o ipconfig -Ld:/RCompile/CRANpkg/extralibs64/local/lib/i386 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/PROGRA~1/R/R-31~1.2/bin/i386 -lR
mean.o:mean.c:<.text+0x1b>: undefined references to 'gsl_stats_mean'
collect2: ld returned 1 exit status
In the command Prompt I am giving the following command also:
>gcc mean.c -Wall -I"C:/Program Files/GnuWin32/include/gsl" -L"C:/Program Files/GnuWin32/lib" -lgslcblas -lgsl -lm -o mean
I have tried all the other permutations of putting mean.c in between and at the end but the compilation is terminated always, giving the message "ld returned 1 exit status". Sometimes giving the message 'R.h' - No such file or directory.
I have also edited the PATH and included GnuWin32 folder and its constituents.
Am I proceeding in the right direction?
Any help would be greatly appreciated, Thanks!
--Ankit
The RcppGSL package has working examples in the included and fully-working package as well as in a simpler spline example. In short, you need to supply a bit more to make and the compiler.
But as Martin Morgan noted in the comment, you also haven't exactly motivated why you would want to call the mean function from the GSL from R...

C - undefined reference to "sqrt" even with '-lm'

I try to compile a library in C that need "math.h", here is the begining of the .c file:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "sparse_matrix.h"
...
and I compile with this command:
gcc -c ./sparse_matrix.c -o sparse_matrix.o -lm -Wall -pedantic -std=c99 -g -O
But even with the #include done and the flag -lm after the file (I've tried at the end of the line but nothing changed) I still get the error:
undefined reference to « sqrt »
collect2: error: ld returned 1 exit status
I don't get it after an hour of googling the issue.
I'm working with gcc 4.9 under ubuntu 14.10 (utopic unicorn).
Thank for any help in advance!
I don't think that is the command you're running (well, it may be one of them, but it's certainly not the one causing your error).
The -c option to gcc tells it to only create the object files (and you're specifically sending the output to sparse_matrix.o, an object file rather than an executable one).
In that case, the linker should not be called at all.
In fact, with a dummy sparse_matrix.c of:
#include <math.h>
int main(void) {
return (int)(sqrt(16.0));
}
your command works fine and, when I complete the process with:
pax> gcc -o sparse_matrix sparse_matrix.o -lm
pax> ./sparse_matrix
pax> echo $?
4
you can see that it also runs just fine.
It may be that you're leaving off the linker flags (such as -lm) from the actual link stage, which would cause this problem. They should have no effect on the compilation stage (unless they affect both compile and link stages but -l isn't one of those).
And, by "leaving off", I also include the possibility of "misplacing". Some linkers are positional in the way they handle libraries in that they will only extract objects from libraries if they satisfy an undefined symbol at the point where they're listed.
So, the command:
linker sparse_matrix.o -lm ...
would work because the .o file introduces an unsatisfied reference to sqrt, which is satisfied by libm. If your linker is positional, then:
linker -lm sparse_matrix.o ...
wouldn't work because, at the time of processing libm, there were no unsatisfied symbols so nothing was extracted. The undefined reference to sqrt is then introduced after that point and there are no other objects or libraries to satisfy it.
Whether ld or the gcc linker stage has that limitation, I don't know, I'm just raising the possibility as something to watch out for.

Undefined reference to WinMain#16 when using SDL

I've been having a lot of trouble getting everything working so that I can start developing on Windows, as apposed to Linux, which is what I normally use when coding. I'm having a rather bizarre issue when trying to compile an SDL program. As soon as I include the SDL library, the program refuses to compile, giving me this error:
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../libmingw32.a<main.o>: In function 'main':
C:\MinGW\msys\1.0\src\mingwrt/../mingw/main.c:73: undefined reference to 'WinMain#16'
collect2: ld returned 1 exist status
I am using MinGW on console.
To give an example, using
gcc -o test main.c
This compiles fine:
#include <stdio.h>
#include <stdlib.h>
int main(int argv, char **argc)
{
printf("Hello, world!\n");
return 0;
}
But as soon as I add #include (even without any SDL functions being called) I get the error mentioned above
Using:
gcc -o test main.c -lSDL
This fails to compile:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
int main(int argv, char **argc)
{
printf("Hello, world!\n");
return 0;
}
Any help would be greatly appreciated! I read that this was a common issue for people who forget to have a main function, but obviously that's not my issue. I also heard that WinMain is the main function used when dealing with Windows graphical programs, but that's never been an issue for me in the past when I used to develop in Windows more.
I did a little bit of searching for some more information on this error, and I found this page which includes the following informaion:
The only trick in getting this to compile now is to add the include path (eg: -I../SDL/include), the linker path (eg: -L../SDL/lib), and then finally adding the libraries themselves in the right order. Use:
-lmingw32 -lSDLmain -lSDL
Also, don't forget to add the -mwindows flag, if your IDE doesn't add it automatically (in addition to whatever other libraries you want to link). If you don't put them in the right order, you'll get a linker error complaining about the missing symbol WinMain#16.
Try recompiling with those flags above and see whether that makes a difference.

Not able to link dependent header files in cpp file for Rcpp

Based on this tutorial - http://www.r-bloggers.com/using-r-callling-c-code-with-rcpp/
I was trying to call a C function from R.
The C code have following dependencies and it works perfectly after compiling the C code
#include <json/json.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <time.h>
#include <math.h>
#include <Rcpp.h>
Now when i am trying to load the so file , i am seeing the below error.
dyn.load("storage.so")
Error in dyn.load("storage.so") :
unable to load shared object '/home/algotree/Rcode/storage.so':
/home/algotree/Rcode/storage.so: undefined symbol: json_object_array_length
Seems R is not able to link the rest of header files.
How can I fix it?
This has nothing to do with Rcpp (for which we also provide ample documentation regarding use on its own, in package, via inline, ...).
You seem to use JSON-parsing functionality, but apparently have not linked to a JSON-parser library corresponding to the header json/json.h you included.
Apart from this question being incomplete in its code example and hence not reproducible, I see two issues here:
learn the ropes about C/C++ program using libraries, and
apply this to the R context.
As you using JSON and Curl based on your headers, you could (and probably should) study the corresponding packages like RJSONIO and RCurl.
If you know what is going there and understand the mechanics, you can then use Rcpp to provide the new functionality you are seeking. But just by throwing Rcpp in the mix, these issues do not address themselves. You need to understand how in include headers and link libraries.
Here is how i solved the issue , for running the code i had to add -lcurl and -ljson as command link arguement. So the command R CMD SHLIB should have executed is the below commands
g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/include/ -fpic -O3 -pipe -g -c storage.cpp -o storage.o
g++ -shared -o storage.so storage.o -L/usr/lib -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/lib64/R/lib -lcurl -ljson -lR
This can be done by editing the PKG_LIBS flags.

Resources