Background : Trying to setup a cross compiler environment for arm target (TQ2440/Mini2440)
On HOST running Red Hat:
Wrote a simple hello program
gcc -o hello hello.c
compiles successfully
./hello
displays the hello world message
rm hello
arm-linux-gcc -o hello hello.c
file hello
It says 32bit compiled for ARM compatible for Linux 2.0.0
Transfer the "hello" binary file to TARGET
chmod a+x hello
./hello
The problem:
/bin/sh: ./hello: not found
Can anyone point out my mistake or what am I missing here?
I executed ldd on host: ldd hello and I got:
/usr/local/arm/3.3.2/bin/ldd: line 1:
/usr/local/arm/3.3.2/lib/ld-linux.so.2: cannot execute binary file
/usr/local/arm/3.3.2/bin/ldd: line 1:
/usr/local/arm/3.3.2/lib/ld-linux.so.2: cannot execute binary file
ldd: /usr/local/arm/3.3.2/lib/ld-linux.so.2 exited with unknown exit code (126)
Solved.
I was transfering the file through ftp. You need to enter bin to switch to binary transfer mode. Works fine now.
Try running ldd hello and see if it complains about any missing dynamic libraries.
Related
I am learning C and I have a simple hello world program that I am trying to run on Windows 10. Here is the code:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
I have installed GCC compiler and I tried the following in order to run it in the command prompt:
gcc hello.c
a
I also tried:
gcc hello.c
./a.exe
and:
gcc hello.c
./a
and:
gcc hello.c -o hello
./hello
The program does not run displaying hello, world and it gives the following error:
bash: a.exe: command not found
What am I doing wrong and how can I run the program after the compilation?
It appears that your compilation succeeded successfully.
See if there is an a.out or a.exe file present, as you didn't indicate a non-default executable name.
Note that running a alone typically won't do anything, because it is highly unlikely that your executable is on the bash PATH. This means you need to run ./a.out or ./a (depending on base operating system).
Binary executables under windows typically must have .exe extension to be recognized as such.
I am not sure if gcc under windows adds the right extension automaticaly when outputting executables.
I would try:
gcc hello.c -o hello.exe
./hello.exe
Trying to compile a source code written in C.
Location of the code is: C:\Users\Chris\Documents\prog\c\learn\GOODBYE.C
In CMD I typed the code: gcc goodbye.c -o goodbye
Got this error:
gcc: error: goodbye.c: No such file or directory
gcc: fatal error: no input files
compilation terminated.
I wanted the output to be named goodbye.
How do I fix this?
Make sure that you are running gcc goodbye.c -o goodbye while you are in the C:\Users\Chris\Documents\prog\c\learn\ directory.
If the c file is named GOODBYE.c then you should run gcc GOODBYE.c -o goodbye
Enter the name of the directory in which the program is located. Like if the program is in "D" drive, then open the VS Code terminal and enter "D:" (without quotation) and hit enter, and then rerun the code like this.
what if you run gcc "C:\Users\Chris\Documents\prog\c\learn\GOODBYE.C" -o goodbye
I guess it could be the case sensitivity. Either rename the file or run gcc GOODBYE.C -o goodbye
I'm new to C, and I understand that both of those commands accomplish the same thing, but does one of them do something different than the other along the way?
First of all, if you are using make then for hello.c you will call make as make hello and not make hello.c. Also, note that make is most of the times used with a Makefile. Nonetheless, you can build executable binary from single source file using make as you have shown.
If the executable (hello) does not exist, then both will have same effect - create the executable from source file.
However, if the executable already exists, then make will run the build commands only if it thinks that the source code has changed after last build, whereas cc will always do the build.
For example:
$ make hello
cc hello.c -o hello
$ make hello
make: 'hello' is up to date. # make does not think source file has changed
$ touch hello.c # Update the timestamp of hello.c
$ make hello
cc hello.c -o hello # make thinks source file changed. Builds again
$
However, cc will not check if the source has changed or not. It will always do the required build.
$ cc hello.c -o hello
$ ls -l hello | cut -d ' ' -f '8-'
12:18 hello
$ cc hello.c -o hello # Build again without changing source
$ ls -l hello | cut -d ' ' -f '8-'
12:21 hello # hello was built again
$
Above description was for GNU make and GNU cc. Not sure about other implementations.
P.S.: make is not a compiler. It only calls the compiler when it thinks it should, as seen in the example above. Whereas cc is a compiler.
P.S. If you run cc hello.c, the excutable is named a.out, and not hello.
make(1) is a program that will run commands. You create a file named "Makefile" in a directory. The Makefile has a recipe with a specific syntax that is beyond the scope of this answer. You then invoke the make command in the directory with the file named Makefile
cc $filename will invoke a C compiler upon the $filename
cc or gcc or clang will invoke the compiler.
make is used when you have many files to compile.
In make you will give name of .o file and include header file location.
I am new to C. Here is my "Hello, World!" program.
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
After I try to run it using Terminal it says:
/Users/macbook/Desktop/peng/Untitled1
-bash: /Users/macbook/Desktop/peng/Untitled1: Permission denied
Why?
First save your program as program.c.
Now you need the compiler, so you need to go to App Store and install Xcode which is Apple's compiler and development tools. How can you find App Store? Do a "Spotlight Search" by typing ⌘Space and start typing App Store and hit Enter when it guesses correctly.
App Store looks like this:
Xcode looks like this on App Store:
Then you need to install the command-line tools in Terminal. How can you start Terminal? You need to do another "Spotlight Search", which means you type ⌘Space and start typing Terminal and hit Enter when it guesses Terminal.
Now install the command-line tools like this:
xcode-select --install
Then you can compile your code with by simply running gcc as in the next line without having to fire up the big, ugly software development GUI called Xcode:
gcc -Wall -o program program.c
Note: On newer versions of OS X, you would use clang instead of gcc, like this:
clang program.c -o program
Then you can run it with:
./program
Hello, World!
If your program is C++, you'll probably want to use one of these commands:
clang++ -o program program.cpp
g++ -std=c++11 -o program program.cpp
g++-7 -std=c++11 -o program program.cpp
First make sure you correct your program:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n"); //printf instead of pintf
return 0;
}
Save the file as HelloWorld.c and type in the terminal:
gcc -o HelloWorld HelloWorld.c
Afterwards, just run the executable like this:
./HelloWorld
You should be seeing Hello, World!
A "C-program" is not supposed to be run. It is meant to be compiled into an "executable" program which then can be run from your terminal. You need a compiler for that.
Oh, and the answer to your last question ("Why?") is that the file you are trying to execute doesn't have the executable rights set (which a compiler usually does automatically with the binary, which let's infer that you were trying to run the source code as a script, hence the hint at compiling.)
This is Working in 2019
By default, you can compile your name.c using the terminal:
cc name.c
And if you need to run, just write
./name.out
To do this:
Open the terminal
Type in the terminal: nano ; which is a text editor available for the terminal. When you do this, something like this would appear.
Here you can type in your C program
Type in Ctrl + X → which means to exit.
save the file by typing in Y to save the file
Type the file name; e.g., helloStack.c (don't forget to add .c)
When this appears, type in gcc helloStack.c
And then ./a.out: this should give you your result!
For compiling a C program on your latest macOS, just type the following in the terminal after saving the file with a .c extension and on reaching the path where the file is saved:
cc yourfilename.c
Once you have checked all the errors after compilation (if any), type the following for executing the code:
./a.out
These commands are tested on macOS v10.14 (Mojave) and are working perfectly fine.
To compile a C program in macOS, simply follow the below steps
Using the cd command in terminal, go to your C program location and then type the command present below:
make filename
then type
./filename
The answer is chmod 755 hello - it makes the file executable... I had same problem on macOS, which is now solved.
nano hello.c
make hello
chmod 755 hello
Then you run it by ./hello
clang --version
Output:
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Nothing was installed. nano make (clang) chmod - all inside macOS already.
On Mac, GCC (executable gcc) is installed by default in /usr/local/bin.
To run C:
gcc -o tutor tutor.c
First you need to install a GCC compiler for Mac (google it and install it from the Internet)
Remember the path where you are storing the C file
Go to Terminal and set the path
E.g., if you have saved in a new folder ProgramC in the Document folder.
Then type this in Terminal:
cd Document
cd ProgramC
Now you can see that you are in folder where you have saved your C program (let you saved your program as Hello.c)
Now compile your program
make Hello
./hello
I'm trying to compile my first "Hello World" application using GCC (build-on clang and stand-alone GCC-4.9.2) without any success:
OS version is OS X Yosemite 10.10.2.
I'm using the following code (main.c):
#include <stdlib.h>
#include <stdio.h>
int main (){
printf("Hello World!");
getchar();
return 0;
}
Then I compile it in terminal with a command (clang shipped with XCode):
gcc -o hello -c main.c
As a result I got following error whe running compiled hello file:
MBP-Andrii:cTest andrii$ gcc -o hello -c main.c
MBP-Andrii:cTest andrii$ hello
-bash: /Users/andrii/avrTest/cTest/hello: Permission denied
If I change the permissions for hello file to 777 and a new error again:
MBP-Andrii:cTest andrii$ chmod 777 hello
MBP-Andrii:cTest andrii$ hello
Killed: 9
MBP-Andrii:cTest andrii$
The same thing happens with stand-alone GCC-4.9.2.
I guess It might be something related to the output binary format or some missing flags for compiler.
Remove the -c from the command you're using to compile the application.
The -c tells the compiler to only compile and assemble but not link. You are not actually creating an application when using -c.
See the GCC Manual:
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
By default, the object file name for a source file is made by replacing the suffix ‘.c’, ‘.i’, ‘.s’, etc., with ‘.o’.
Unrecognized input files, not requiring compilation or assembly, are ignored.
Try executing it by typing ./hello