0

This a test with the GNU Scientific Library release 2.4. This is example 9.9 from documentation (Jun 14, 2017):

#include <stdio.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_permutation.h>

int
main (void)
{
const size_t N = 10;
const gsl_rng_type * T;
gsl_rng * r;

gsl_permutation * p = gsl_permutation_alloc (N);
gsl_permutation * q = gsl_permutation_alloc (N);

gsl_rng_env_setup();
T = gsl_rng_default;
r = gsl_rng_alloc (T);

printf ("initial permutation:");
gsl_permutation_init (p);
gsl_permutation_fprintf (stdout, p, " %u");
printf ("\n");

printf (" random permutation:");
gsl_ran_shuffle (r, p->data, N, sizeof(size_t));
gsl_permutation_fprintf (stdout, p, " %u");
printf ("\n");

printf ("inverse permutation:");
gsl_permutation_inverse (q, p);
gsl_permutation_fprintf (stdout, q, " %u");
printf ("\n");

gsl_permutation_free (p);
gsl_permutation_free (q);
gsl_rng_free (r);

return 0;
}

Linker settings

Build log:

-------------- Build: Debug in example (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -g -I../../../../usr/include/gsl -c /home/lucas/Desktop/example/main.cpp -o obj/Debug/home/lucas/Desktop/example/main.o
g++  -o bin/Debug/example obj/Debug/home/lucas/Desktop/example/main.o   ../../../../usr/include/gsl/gsl_permutation.h ../../../../usr/include/gsl/gsl_randist.h ../../../../usr/include/gsl/gsl_rng.h
obj/Debug/home/lucas/Desktop/example/main.o: In function `main':
/home/lucas/Desktop/example/main.cpp:13: undefined reference to `gsl_permutation_alloc'
/home/lucas/Desktop/example/main.cpp:14: undefined reference to `gsl_permutation_alloc'
/home/lucas/Desktop/example/main.cpp:16: undefined reference to `gsl_rng_env_setup'
/home/lucas/Desktop/example/main.cpp:17: undefined reference to `gsl_rng_default'
/home/lucas/Desktop/example/main.cpp:18: undefined reference to `gsl_rng_alloc'
/home/lucas/Desktop/example/main.cpp:21: undefined reference to `gsl_permutation_init'
/home/lucas/Desktop/example/main.cpp:22: undefined reference to `gsl_permutation_fprintf'
/home/lucas/Desktop/example/main.cpp:26: undefined reference to `gsl_ran_shuffle'
/home/lucas/Desktop/example/main.cpp:27: undefined reference to `gsl_permutation_fprintf'
/home/lucas/Desktop/example/main.cpp:31: undefined reference to `gsl_permutation_inverse'
/home/lucas/Desktop/example/main.cpp:32: undefined reference to `gsl_permutation_fprintf'
/home/lucas/Desktop/example/main.cpp:35: undefined reference to `gsl_permutation_free'
/home/lucas/Desktop/example/main.cpp:36: undefined reference to `gsl_permutation_free'
/home/lucas/Desktop/example/main.cpp:37: undefined reference to `gsl_rng_free'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
15 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Is there a way to trace this error? Is this an issue with link libraries, search directories, compiler configurations, project headers? I'm using Code::Blocks 16.01

user3083324
  • 585
  • 8
  • 23
  • ***Is this an issue with link libraries*** From your output I don't see you linking to the gsl library like in this example: https://www.gnu.org/software/gsl/manual/html_node/Linking-programs-with-the-library.html#Linking-programs-with-the-library – drescherjm Jun 28 '17 at 19:24
  • Your failing link command shows that you have neglected to link `libgsl` and on the other hand are attempting to link header files, which isn't possible. See [this answer](https://stackoverflow.com/a/12574400/1362568) to [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/q/12573816/1362568) – Mike Kinghan Jun 28 '17 at 19:28
  • Linking libgsl.a solved the problem. 1. Why I didn't get an undefined reference for gsl_rng? 2.Why the compiler doesn't automatically link the necessary libraries? Is this an issue with g++ or with Code::Blocks? – user3083324 Jun 29 '17 at 00:27
  • The linker does not know what "the necessary" libraries are unless you tell it. The compiler includes the header files you tell it to. The linker links the libraries you tell it to. There is no inference from the name of any header file to the existence or name of any library. You did not get an undefined reference for `gsl_rng` because it is the name of a type, for which the compiler found a definition in `gsl/gsl_rng.h`; it is not the name of any linkable object (function or variable), defined in `libgsl.a`. – Mike Kinghan Jun 29 '17 at 09:38
  • @MikeKinghan In Windows cygwin64 there's a libgsl.dll.a, is this the Windows equivalent of libgsl.a? – user3083324 Jun 29 '17 at 14:08
  • No it's not. To link `libgsl` put `-lgsl` in **Other linker options** (and nothing in **Link libraries**) and the linker will locate the right file, if the library is installed on the system, whatever your system may be. Similarly `-lblas` to link `libblas`. `-I../../../../usr/include/gsl` is an incorrect include-path for the compiler, but unnecessary so you can delete it from your settings. Your program is written in C, not C++, so best compile it with `gcc`, not `g++`. – Mike Kinghan Jun 29 '17 at 16:58

0 Answers0