Need help using GNU scientific library

  • Thread starter quasar_4
  • Start date
  • Tags
    Scientific
In summary, you installed the gsl library using MacPorts on your computer, but you are having difficulty running a sample program from the manual. You need to compile the program and link it with the gsl library.
  • #1
quasar_4
290
0
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):

#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:
Technology news on Phys.org
  • #2
quasar_4 said:
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):

#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).

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:
  • #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.
 
  • #4
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:
$ gcc -Wall -I/usr/local/include GLS-mc-demo.c -L/usr/local/lib -lgsl
$ ./a.out
 
  • #5
quasar_4 said:
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.

I like Serena said:
Try it without the -c flag.
That flag means "compile-only", but your program also needs to be "linked".

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!
 
  • #6
quasar_4 said:
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.

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:
  • #7
SixNein said:
you have a permission issue with your file system.

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.
 
  • #8
I like Serena said:
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.

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.
 
  • #9
SixNein said:
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.

Please try the command lines used yourself.
You'll see that you will get the same permission denied error.
 
  • #10
I like Serena said:
Please try the command lines used yourself.
You'll see that you will get the same permission denied error.

[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:

#include <stdio.h>

int main()
{

printf("Watch me run\n");return 0;
}
This was compiled an ran in my home directory.
 
  • #11
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.)
 
  • #12
may want to check permissions on the a.out

ls -al 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:

chmod 755 a.out

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.
 
  • #13
SixNein said:
you can set permissions with:
$ chmod 755 a.out

If he does that, he'll get:
bash: ./a.out: cannot execute binary file
 
  • #14
I like Serena said:
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.)

Ahhh he didn't copy and paste the line I wrote.
 
  • #15
I like Serena said:
If he does that, he'll get:
bash: ./a.out: cannot execute binary file

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.
 
  • #16
SixNein said:
Ahhh he didn't copy and paste the line I wrote.

No he didn't. :smile:
 

1. What is the GNU scientific library?

The GNU scientific library (GSL) is a collection of numerical routines for scientific computing written in the C programming language. It provides a wide range of functions for linear algebra, interpolation, random number generation, and more.

2. How do I install the GNU scientific library?

The installation process for the GNU scientific library varies depending on your operating system. On Linux, you can use your package manager to install it. On Windows, you can download a pre-compiled version or compile from source using a compiler like MinGW.

3. How do I use the functions in the GNU scientific library?

To use the GNU scientific library, you will need to link it to your C program and include the appropriate header files. You can then call the functions in your code to perform various mathematical operations. It is recommended to refer to the documentation for specific usage instructions.

4. Can I use the GNU scientific library in other programming languages?

Yes, the GNU scientific library has bindings for several programming languages, including C++, Python, and Fortran. This allows you to use the functions in the library in your preferred programming language.

5. Is the GNU scientific library free to use?

Yes, the GNU scientific library is an open-source software released under the GNU General Public License. This means it is free to use, modify, and distribute for both commercial and non-commercial purposes.

Similar threads

  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
10
Views
5K
  • Programming and Computer Science
Replies
6
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
Replies
14
Views
3K
Replies
7
Views
12K
Back
Top