Need help using GNU scientific library

  • Thread starter quasar_4
  • Start date
  • #1
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): [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:

Answers and Replies

  • #2
SixNein
Gold Member
78
16
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).

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
290
0
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
I like Serena
Homework Helper
6,579
176
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
AlephZero
Science Advisor
Homework Helper
7,002
293
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.

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
SixNein
Gold Member
78
16
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
I like Serena
Homework Helper
6,579
176
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
SixNein
Gold Member
78
16
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
I like Serena
Homework Helper
6,579
176
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
SixNein
Gold Member
78
16
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
I like Serena
Homework Helper
6,579
176
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
SixNein
Gold Member
78
16
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
I like Serena
Homework Helper
6,579
176
you can set permissions with:
$ chmod 755 a.out

If he does that, he'll get:
bash: ./a.out: cannot execute binary file
 
  • #14
SixNein
Gold Member
78
16
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
SixNein
Gold Member
78
16
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
I like Serena
Homework Helper
6,579
176
Ahhh he didn't copy and paste the line I wrote.

No he didn't. :smile:
 

Related Threads on Need help using GNU scientific library

  • Last Post
Replies
3
Views
3K
Replies
10
Views
4K
  • Last Post
Replies
22
Views
5K
Replies
9
Views
2K
Replies
1
Views
3K
Replies
7
Views
4K
  • Last Post
Replies
0
Views
460
  • Last Post
Replies
2
Views
3K
Top