Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help using GNU scientific library

  1. Aug 11, 2011 #1
    Hi,

    I'm stumped trying to learn how to use the GNU scientific library.

    I installed X11, Xcode and gsl library using MacPorts on my computer which is running Mac OSX 10.6.8. I've used Xcode plenty in the past year for generic (simple) c programs and it's worked fine, so I think all the installs worked.

    All I am trying to do at the moment is run the first sample in the GNU scientific library manual (also located at http://www.gnu.org/software/gsl/manual/html_node/An-Example-Program.html): [Broken]

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

    int
    main (void)
    {
    double x = 5.0;
    double y = gsl_sf_bessel_J0 (x);
    printf ("J0(%g) = %.18e\n", x, y);
    return 0;
    }

    I saved this snippet as GSL-demo.c on my computer and in my terminal window I type:
    $ gcc -Wall -I/usr/local/include -c GLS-mc-demo.c

    which seems to compile fine. When I try to run it,

    $ ./a.out

    I get:

    -bash: ./a.out: No such file or directory

    How do I actually run the code? And what does -c mean? (I'm totally new to unix too, for the most part).
     
    Last edited by a moderator: May 5, 2017
  2. jcsd
  3. Aug 12, 2011 #2

    SixNein

    User Avatar
    Gold Member

    Try
    $ gcc -Wall -I/usr/local/include -o a.out GLS-mc-demo.c

    then try to run a.out


    -c means compile
     
    Last edited by a moderator: May 5, 2017
  4. Aug 12, 2011 #3
    It still doesn't run. I get:

    $ gcc -Wall -I/usr/local/include -o a.out -c GLS-mc-demo.c
    $ ./a.out
    -bash: ./a.out: Permission denied
    $

    I tried putting -c before -o as well and got the same result.
     
  5. Aug 12, 2011 #4

    I like Serena

    User Avatar
    Homework Helper

    Try it without the -c flag.
    That flag means "compile-only", but your program also needs to be "linked".
    Btw, no doubt you will also have to link against the gsl-library.

    This means you need to do something like:
    Code (Text):
    $ gcc -Wall -I/usr/local/include GLS-mc-demo.c -L/usr/local/lib -lgsl
    $ ./a.out
     
  6. Aug 12, 2011 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    Yep, if you specify both -o and -c, you are just renaming the "object code" demo.o file as a.out.

    It's hard to think of any "simple" situation where doing that would be useful. The only "complicated" situation I can think of iis if you had source files in different languages with similar names (for example demo.c and demo.f), and obviously you can't have two different files both called demo.o in the same directory. But it would be better to fix that problem by remanimg the files, or changing the directory structure for the project!
     
  7. Aug 12, 2011 #6

    SixNein

    User Avatar
    Gold Member

    You have a permission issue with your file system. Check the directory to make sure you have right to execute. And don't use -c.

    You could try
    $ gcc -Wall -I/usr/local/include -o /home/YOURUSERNAME/a.out GLS-mc-demo.c
    $ /home/YOURUSERNAME/a.out

    May be selinux causing the issue for the permission denied by setting ownership to that of the directory. Make sure you change the YOURUSERNAME to whatever username your using.
     
    Last edited: Aug 12, 2011
  8. Aug 12, 2011 #7

    I like Serena

    User Avatar
    Homework Helper

    Nope. That won't be it.
    Obviously a.out could be written, otherwise gcc would have complained.
    gcc itself sets the proper executable bits on a.out, so it will run.
    However, what should be an object file, is written to the file a.out, but that cannot be executed, hence the permission error.
    This was already remarked upon by AlephZero.
     
  9. Aug 12, 2011 #8

    SixNein

    User Avatar
    Gold Member

    Permission denied is a result of the file system, and it has nothing to do with GCC. Most likely, SElinux is setting the group to that of the directory.
     
  10. Aug 13, 2011 #9

    I like Serena

    User Avatar
    Homework Helper

    Please try the command lines used yourself.
    You'll see that you will get the same permission denied error.
     
  11. Aug 13, 2011 #10

    SixNein

    User Avatar
    Gold Member

    [Maven@localhost ~]$ gcc -Wall -I/usr/local/include -o test test.c
    [Maven@localhost ~]$ ./test
    Watch me run
    [Maven@localhost ~]$ gcc -Wall -I/usr/local/include -o a.out test.c
    [Maven@localhost ~]$ ./a.out
    Watch me run
    [Maven@localhost ~]$

    source:


    This was compiled an ran in my home directory.
     
  12. Aug 13, 2011 #11

    I like Serena

    User Avatar
    Homework Helper

    Now try it with:

    [Maven@localhost ~]$ gcc -Wall -I/usr/local/include -o a.out -c test.c
    [Maven@localhost ~]$ ./a.out

    Note the extra "-c".
    (That is what quasar_4 did.)
     
  13. Aug 13, 2011 #12

    SixNein

    User Avatar
    Gold Member

    may want to check permissions on the a.out

    You should see something like this:

    -rwxrwxr-x. 1 Maven Maven 4642 Aug 13 06:14 a.out

    if you don't see an x, you can't execute the program.

    you can set permissions with:

    If you are compiling the program on removable storage, external hard drive, etc, you may need to remount the drive with the exec option included.
     
  14. Aug 13, 2011 #13

    I like Serena

    User Avatar
    Homework Helper

    If he does that, he'll get:
    bash: ./a.out: cannot execute binary file
     
  15. Aug 13, 2011 #14

    SixNein

    User Avatar
    Gold Member

    Ahhh he didn't copy and paste the line I wrote.
     
  16. Aug 13, 2011 #15

    SixNein

    User Avatar
    Gold Member

    There are some issues that can come up with permissions and especially on mounted drives with exec off. I figured it was something like that. I assumed he tried the line I gave him, but I guess not.
     
  17. Aug 13, 2011 #16

    I like Serena

    User Avatar
    Homework Helper

    No he didn't. :smile:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Need help using GNU scientific library
  1. GNU fortran help (Replies: 22)

Loading...