Ubuntu:: ./program: Permission Denied - c

Firstly, my problem is similar to this: Ubuntu says "bash: ./program Permission denied"
However, I feel the need to further clarify.
When I compile my program using:
gcc -c file.c -o file
and run
./file
I get this error:
bash:./file: Permission denied
When I use
chmod u+x file
and then run
./file
I get this error:
bash: ./file: cannot execute binary file: Exec format error
However, when I compile using
gcc file.c -o file <br/>
My program runs perfectly well using
./file
Can someone point out what is the problem with using the -c argument with gcc?

Type gcc --help to see some help.
-c Only run preprocess, compile, and assemble steps
This means that, when run with this option, GCC doesn't link the executable with any (even system) libraries.
In short, to run a program, the OS needs a starting point, which is located in some system library. Since in your case GCC isn't linking the executable with anything, the OS doesn't know how to run the file, where to start.

Related

win10 gdb not in executable format: File format not recognized

I'm a C newbie who recently installed gcc and gdb on windows 10 through MinGW. However, when I tried to debug my program using gdb through win10 cmd I got an error.
Here's what I did on cmd:
gcc ./test.c
gdb ./a.exe
And I got this:
not in executable format: File format not recognized
Then I tried to install gdb on bash as well and did the following:
gcc ./test.c
gdb ./a.out
(gcc through bash would create a.out but would create a.exe through cmd)
This time gdb seems to be able to start the executable file correctly, however when I tried typing in break commands I got another error message:
No symbol table is loaded. Use the "file" command.
Can anyone help to solve the problem?
PS. I've tried using file but it didn't work either.
PSS.after using gcc -g ./test.c the gdb seems to be working correctly on bash. However the windows one still can't run .exe file.

The equivalent of "./" in windows is ".exe" extension?

I'm running in windows and I want to know what is the equivalent of ./my_program in windows?
For example if I compile a file in c with gcc, something like
gcc my_program.c -o my_program
The I 've to launch it with ./my_progrm in Linux,but in windows?
If I compile this : gcc my_program.c -o my_program, after I can't launch, I have to compile in that way : gcc my_program.c -o my_program.exe
I'm using minGW
Thank you
./ is not an instruction to execute a program in Linux, but the path to the program to execute.
Actually, ./my_prog means I want to execute le 'my_prog' program in the current directory (.), but you can also do ../my_dir/my_prog or anything else.
When you do ls -l, the shell understand /bin/ls -l.
In windows, if you are in the same directory as your .exe, you don't need to provide the path, just do my_program.exe
Actually, the executables files for Linux are binary files and they don't have any extension, but in windows it is .exe

Cygwin Shell: $ make ex1 returns: "The system cannot find the specified file" on Windows 7

I'm following the steps of the online book: "Learn C The Hard Way", and since I'm using Windows 7, I've installed Cygwin to use the Linux commands. But I'm facing a problem just on the first exercise of the book. I'm supposed to put the following command on the shell:
$ make ex1
After creating a ex1.c file on the folder. The command should give me:
cc ex1.c -o ex1
But instead, I'm getting the following message:
$ make ex1
cc ex1.c -o ex1
process_begin: CreateProcess(NULL, cc ex1.c -o ex1, ...) failed.
make (e=2): The system cannot find the specified file.
make: *** [ex1] Error 2
What's wrong?
First of all you should know that you should be running the command in the same directory where the file is. In cygwin, first you will have to locate to the folder in which the file is present, then you can run these make commands. Better since you are using Windows. You should better use any other windows based client for C. But if I were at your place. I would have installed a virtual Linux environment on my local windows computer and would have worked on that. You should try that once. Linux Terminal gives a lot of power to the developer. There are a lot of things which you can do on a terminal which is not supported by cygwin. For compiling C programs on Cygwin, I believe you should check if it supports compiler commands or not. :)
Make is reporting that it can not find cc.
cc is a link to gcc, and it belongs to gcc-core.
$ cygcheck -f /usr/bin/cc
gcc-core-5.4.0-1
To verify if the package is correctly installed
$ cygcheck -c gcc-core
Cygwin Package Information
Package Version Status
gcc-core 5.4.0-1 OK
If, as likely, the package is missing, you need to install it with the cygwin setup.

How to execute and compile a program in Xcode via command line in c and where's the executable?

I'm trying to execute via command line a code written in C. I tried gcc -o file file.c, but it did not work. I need to learn how to compile and execute a code using gcc and llvm without graphical interface. Furthermore when I compile the program I cannot find the executable file in Finder (there's no Developer folder in Library).
Thanks in advance.
You can use xcrun tool:
#/usr/bin/xcrun cc -o file file.c
Note: if you have several Xcode versions you can chose with xcode-select and your command above will use compiler and the rest of the tools from the selected SDK.
If file.c is in the Desktop directory: Did you change to that directory beforehand?
Usually Terminal.app starts in the home directory, e.g.: /Users/yourname
To get to the Desktop directory:
cd ~/Desktop
Then check if the source file is there:
ls -l file.c
Then try again to compile:
gcc -o file file.c
Check for any error messages. If no output is given everything is fine and there should be an executable which can be (surprise!) executed:
ls -l file
./file

Can not find linux/modversions.h

I am trying to install the driver for a serial device, and when I run the installation executable I get this error:
cc -DLINUX -c -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O2 -fomit-frame-pointer -pipe -m64 -mcmodel=kernel -I/usr/src/linux-3.8.0-27-generic/include -I/usr/src/linux-2.4/include -I/usr/src/linux/include -D__SMP__ npreal2.c
npreal2.c:40:31: **fatal error: linux/modversions.h: No such file or directory**
compilation terminated.
I don't find any solutions to this after searching the forums. I noticed that there is a modversions.h in the /usr/src/linux-3.8.0-27-generic/include/config , but not in the linux folder.
Please help!
Try passing -I /usr/src/linux-3.8.0-27-generic/include/config as an argument to make?
or
Check if the header is a part of a certain package and update the package.
You can compile modversions on your system by navigating to the linux directory (usually usr/src/linux). Inside the linux source directory, there should be a file called Rules.make. Inside this make file are build commands for making modversions.h. You can make it by running:
make update-modverfile
Now, while this will make the modversions.h library, if you compile it with a newer compiler than the libraries that this file relies on, many times you will get an error when trying to run a program that uses this header. This then turns into a nightmare.
Another method, I tried it successfully with Xubuntu 13.10:
Open /etc/default/grub
Add this Line and save it.
GRUB_CMDLINE_LINUX="CONFIG_MODVERSIONS=true"
reboot
(no, sudo update-grub,ok)
open a terminal window, enjoy.
locate modversions.h
(Please don't forget modversion'S')

Resources