0

I am trying to test the installation of gsl in my Macbook. I followed instructions from this (1) and this (2) website for the installation of gsl.

When I try to install gsl using brew install gsl, I get the message:

Warning: gsl 2.5 is already installed and up-to-date

But, the above message is not a big cause of worry. To check if gsl is installed properly, I am trying to compile a test code which is given below (main.c). I have trouble when I try to compile this code.

#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

int main( int argc, const char *argv[] ) {
    double x = 5.0 ;
    double y = gsl_sf_bessel_J0( x ) ;
    printf("J0(%g) = % .18e\n", x, y ) ;
    return 0 ;
}

I am trying to compile the above code using gcc -Wall -I/usr/local/inlcude main.c. Below is the error message that I am getting.

Undefined symbols for architecture x86_64:
  "_gsl_sf_bessel_J0", referenced from:
      _main in ccHCdcTr.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

I read the answers given in this stackoverflow post (because it also refers to the aforementioned error ld: symbol(s) not found for architecture x86_64 ). And, I tried to compile the code using gcc -m32 -Wall -I/usr/local/inlcude main.c. But now, I get more errors:

 /var/folders/mp/z7xpkw3538z71904cdqhztv00000gn/T//ccrjsmhl.s: 
   46:11: warning: section "__textcoal_nt" is deprecated
        .section __TEXT,__textcoal_nt,coalesced,pure_instructions
                 ^      ~~~~~~~~~~~~~
/var/folders/mp/z7xpkw3538z71904cdqhztv00000gn/T//ccrjsmhl.s:46:11: note: change section name to "__text"
        .section 
    __TEXT,__textcoal_nt,coalesced,pure_instructions
                 ^      ~~~~~~~~~~~~~
ld: warning: The i386 architecture is deprecated for macOS (remove from the Xcode build setting: ARCHS)
Undefined symbols for architecture i386:
  "_gsl_sf_bessel_J0", referenced from:
      _main in ccAmYmtF.o
ld: symbol(s) not found for architecture i386
collect2: error: ld returned 1 exit status

In both cases, I do not see any object file ( *.o ) in my working directory after compilation. How can I get the above mentioned code to run properly? How can I check if my gsl is installed correctly? I am working with macOS Mojave Version 10.14.3 .

Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52
  • You are missing a `-lgsl` flag. – user58697 Feb 14 '19 at 02:29
  • user58697 : Thanks for the answer. I used `gcc -Wall -lgsl -I/usr/local/inlcude main.c` , and I do not see any error now. But, I still do not see any `main.o` file (which is what http://connor-johnson.com/2014/10/12/using-the-gnu-scientific-library-on-a-mac/ says that I should see) – Siddharth Satpathy Feb 14 '19 at 02:35
  • If you just want to produce a `.o` file, without linking it, specify `-c`. Then you'll get `main.o` instead of `a.out`. But you'll need to link it to produce an executable. – Tom Karzes Feb 14 '19 at 02:50
  • 1
    try `-I/usr/local/include` instead of `-I/usr/local/inlcude`. Although that should be the default so you should not need it at all – M.M Feb 14 '19 at 03:38
  • 1
    `-lgsl` comes after `main.c`, you have put it before – M.M Feb 14 '19 at 03:38
  • @M.M : Thanks for your suggestions. I tried both the suggestions that you gave. Sadly, I still do not have a `.o` object file after compilation. – Siddharth Satpathy Feb 14 '19 at 03:53
  • 1
    Why do you want a `.o` file after compilation? Your question suggests you just want to compile and run `main.c` – M.M Feb 14 '19 at 04:11
  • Also it is not clear why you are trying to use answers from a question about ocaml. The original error message suggests your system is native 64-bit , can you explain why you are trying to change to a 32-bit build instead of just fixing the 64-bit build? Your system probably only has the 64-bit version of gsl installed – M.M Feb 14 '19 at 04:14
  • 1
    It would help to describe what happens when you do exactly `gcc main.c -lgsl`. The expected behaviour is that you get an executable which you can run (not a .o file). – M.M Feb 14 '19 at 04:17
  • @SiddharthSatpathy: On the Linux terminal run command "file libgsl.so" this will give the information on for which architecture your library is compiled. macOS also should have a similar command, Then you can decide on how to compile the main. – Deepak Feb 14 '19 at 05:05
  • If you want an object file, you should just do `gcc -c main.c`. – melpomene Feb 14 '19 at 07:36

1 Answers1

0

I tried the following steps, and it worked. =)

I compiled the file using the following:

gcc -Wall -I/usr/local/include -c main.c -o main.o

And then linked it using the command given below:

gcc -L/usr/local/lib main.o -lgsl -o main.out

Now, when I run the executable main.out from the command line using

./main.out

I get the following output:

J0(5) = -1.775967713143382642e-01
Siddharth Satpathy
  • 2,737
  • 4
  • 29
  • 52