0

I am trying to execute a C program via gcc compiler on ubuntu 18.04

In a folder named 'gsl', I have extracted the gsl.tar file. This is also the folder where I save all my ' '.c programs written in gedit.

When to trying to execute the following via terminal,

gcc -Wall -I/Home/gsl/include -c sample.c

followed by

./a.out

I receive the following error : bash: ./a.out: No such file or directory

I quickly realized this is due to permanent deletion of a file (on my behalf) named a.out from the gsl folder.

To confirm, upon execution of the following commands :

ls -al a.out
chmod +x a.out

I get the same output :

ls: cannot access 'a.out': No such file or directory
chmod: cannot access 'a.out': No such file or directory

What action/step next will suffice to solve this issue?

How can I retrieve the a.out file (permanently deleted on my behalf)?

Thank you

Edit 1 : Removed the link for a question posted on Raspberry Pi community which I found similar to this query but is not, Thanks to @Oscar for pointing it out

Zanna
  • 69,223
  • 56
  • 216
  • 327
kedarb
  • 75
  • 1
  • 12
  • 2
    Remove the `-c` switch - it tells `gcc` to compile (to object code) but stop short of linking to create an executable – steeldriver Jul 25 '19 at 13:01
  • @steeldriver the output of the command after removing -c is as follows : `/tmp/ccfJWetl.o: In function `main': sample.c:(.text+0x23): undefined reference to `gsl_sf_bessel_J0' collect2: error: ld returned 1 exit status` – kedarb Jul 25 '19 at 13:04
  • 2
    So that means your `gcc` recipe is insufficient for building the program - it needs to be linked with some libraries. You will need to be specific about the particular software if you want more useful help. Are you actually trying to build gsl (which is available pre-built from the Ubuntu repository btw)? Is there a Makefile in the directory? – steeldriver Jul 25 '19 at 13:06
  • @steeldriver No, I am NOT trying to build gsl. Due to deletion of a file `a.out` in the gsl folder, I am receiving the such error. As earlier, I was able to execute them successfully. – kedarb Jul 25 '19 at 13:11
  • 1
    @kedar, the similar question you link to, is actually a different case. In that question there is an a.out, but the shell can't execute it because no path to a.out is given and the currenct directory is not in $PATH. You avoid *that* error by prepending ./ to a.out. In your case there is no a.out at all. Also, as steeldriver already noticed, the gcc command you mentioned will not produce an a.out. Was your (now deleted) a.out really the output of compiling sample.c or any of your other .c files in the past? – Oscar Jul 25 '19 at 13:18
  • @Oscar Right. Thank you so much for pointing out the difference. I shall remove the link. No, `a.out` was NOT the output of compiling sample.c as the right output of compiling it should be `sample.o` which in my case, is also missing in the folder. How should I go about solving this ? – kedarb Jul 25 '19 at 13:26
  • @kedar `a.out` is the default name gcc uses when compiling and linking a program. You can override this by using `-o fancyname`. If compiling and linking succeeds, you can run it using `./fancyname` instead of `./a.out`. It looks like you're trying a to compile a part of a C program that needs more than just the code in sample.c. If `gcc -c` doesn't even create sample.o, then you need to look at the output of the gcc command. If it does produce sample.o it needs more to link into a runnable program as it's complaining about undefined references. Can you explain what you're trying to do? – Oscar Jul 25 '19 at 13:38
  • 1
    Have you tried unpacking the gsl.tar file again ? This should repopulate your gls directory with all default files. – Soren A Jul 25 '19 at 13:42
  • @Oscar I am learning to code in C incorporating gsl and fftw libraries for modeling, by the reading their documentation on `https://www.gnu.org/software/gsl/doc/html/usage.html` I also performed a sample test program to initiate my learning. – kedarb Jul 25 '19 at 13:46
  • @SorenA Ohh, I did not considered until now. I shall see and let you know – kedarb Jul 25 '19 at 13:48
  • 1
    @kedar, that documentation assumes you have gsl in it's own directory and you include header files using the `-I` parameter describing the path where it can find that direcory and the `-L` parameter describing the path to the static library `libgsl.a`. Check if these header files (a lot of .h files?) are indeed over there in `/Home/gsl/include/gsl` and also check if you need the `-I` to make gcc find the static library. Then you can try compiling the program again. If it still doesn't work, please show the contents of the tar, where you placed the files, your `sample.c` and the output of gcc. – Oscar Jul 25 '19 at 13:58
  • @Oscar Yes, you are right. I missed out on porper linking. To link against the library I need to specify both the main library and a supporting CBLAS library, on the assigning all the header files are present in the path `/Home/gsl/include/lib`. **gcc -L/Home/gsl/lib sample.o -lgsl -lgslcblas -lm** followed by **./a.out** runs sucessfully. @steeldriver also mentioned about linking earlier. A big Thanks to @steeldriver @Oscar @Soren A for your time and efforts, healthy discussions indeed lead to solutions ! – kedarb Jul 25 '19 at 14:22

1 Answers1

1

This query is resolved now.

Proper linking against library was required to run the ''.c file which uses gsl libraries specifically CBLAS, executing

gcc -L/Home/gsl/lib sample.c -lgsl -lgslcblas -lm

followed by ./a.out displays the right output

A huge Thanks to @steeldriver @Oscar and @Soren A for their time and efforts in walking me step-by-step through this query. Healthy discussions indeed lead us to solutions.

Refer to : Using the gsl Library

kedarb
  • 75
  • 1
  • 12