Hello I have a project written in C that contains four .c files in different directories.These are ipv4_lib.c udp_lib.c projekt_C.c programLib.c
I 've written it in eclipse and everything works fine and it's easy to run,but now I have to run it in console.I have already run programs in console,but they were much easier and usually contained one or two headers in the same directory,so all I had to do was compile each files and run the main one.
But I have no idea how to run project.Is there some command to do that or something like that? Thanks
Look into GCC and Clang:
GCC: https://en.wikipedia.org/wiki/GNU_Compiler_Collection
Clang: https://en.wikipedia.org/wiki/Clang, https://clang.llvm.org/
Both are compilers you can use to compile your source code via the terminal. GCC is has been around longer and has better support but can be a little slow. Clang is newer, so less widely used, but is noticeably faster than GCC at compiling source code.
An example Clang command in your terminal:
clang -o hello hello.c && ./hello
This will compile your hello.c file and give you a hello executable that you can then run. We'll just assume that running the hello program prints Hello, World! to the console.
Related
I am doing the CS50 class, I have installed the cs50.h.
Based on the instructions I used the following command in terminal to compile my simple program and just want to make sure I understand everything im asking terminal to do.
Line is:
gcc -g hello.c -o hello -lcs50 -lm
I know the following*: gcc =
gcc = gnu compiler for C
-g = generate source-level debug information
Hello.c = name of the file we want to compile
-o = write output file
hello = our output file name
Can anyone tell me what -lcs50 and -lm are? My guess is that its calling on the library lcs50 in (-lcs50) but again this is a guess and would like to know for sure.
Everything works as it should with no issues
Thanks,
Mostly correct.
-o is not required to generate the output file, it's only needed to customize the name. (-o and the following name can only appear together).
-lcs50 means "link the library called cs50", not lcs50. It will try to find this file using several different name patterns, e.g. libcs50.so (on Linux), [lib]cs50.dll[.a] (on Windows), libcs50.a (on both), something else on Mac.
-lm links the standard math library, but I don't think you need to manually specify it on most modern GCC distributions.
Yes. For -lm, it's for the maths library, which is not linked by default. This is explained well at Why do you need an explicit `-lm` compiler option.
Just starting in C, and learning the basics.
I have created a simple program that when compiled, would expected to compile as a.out however gcc is compiling as a.exe.
GCC on Windows defaults to creating an file named a.exe on Windows, because the .exe extension is important - a file name a.out would not recognized as an executable program on Windows. The default output could be named a.out.exe but it would be different from a.out anyway, so it is just as sensible to produce a file named a.exe instead.
a.exe is less keypresses, too.
i want ask you for some question what can be incorrect,
i write a program in C and translate it in
gcc -Wall -pedantic
and run
valgrind --simulate-cache=yes --tool=callgrind ./a.out
that create callgrind.out.[pid-number]
and if i run
callgrind_annotate callgrind.out.[pid] main.c
out will be
-- User-annotated source: main.c
No information has been collected for main.c
is it any way how to annotate code and calls of function for program optimalize tools ?
note
gcc -g - pg progrma.c
not working because Apple unsuported debug option on gcc and gprof is unsupported too. And KDE program don`t want run on Mac ...
Thanks everyone to helpful information how to solve it
You're compiling with gprof profiling information when you compile with -pg. valgrind doesn't actually need that data to do it's profiling, what it does need is the debug information.
Using valgrind-HEAD, I took a simple piece of code and compiled it without -g and got the same result as you - i.e. No information has been collected for main.c.
When I compiled with -g, I got useful information about main.c, even when I compiled with optimization I got useful information.
Long and short of it is that you need to compile with -g, not with -pg to get it to work with callgrind.
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 am learning C with the GCC compiler and Geany (Arch Linux, if it makes a difference). However, I am seeing the words compile and build used interchangeably, both in Geany and on the internet. I am asking for clarification that the way I understand the compiling process is correct, because Googling it is just making me more confused.
Say I write a simple helloworld.c file:
#include <stdio.h>
int main(void)
{
printf("Hello world!");
return 0;
}
If I run gcc -c helloworld.c, the compiler produces a helloworld.o object file. Geany calls this process compilation and the compiler says Compilation finished successfully.
Now, if I run gcc -o helloworld helloworld.c, the compiler produces an executable file called helloworld and Geany calls it building. However, the compiler again says Compilation finished successfully.
I understand that the -c option produces an object file, and that multiple of these can be linked together with libraries to produce an executable file, but I am confused about which scenario is compilation and which is building.
Furthermore, if I had just one source file in the project, such as a single helloworld.c file, is gcc -o helloworld helloworld.c enough to turn the source code into an executable?
Thanks
To answer your 2nd question: yes, gcc -o myprog myprog.c is just fine. So is gcc -o myprog *.c or gcc -o myprog foo.c bar.c baz.c.
To answer your first question: technically speaking, there's no word as 'building' :) However, the word 'building' and 'compiling' can be used interchangeably to describe the whole process of producing a final executable from source code.
In a more precise context you would say there is:
preprocessing, when the preprocessor includes header files, expands macros, etc.
parsing, where the parser tokenizes the source text and produces a structured data model (a so-called Abstract Syntax Tree) of the program flow.
compiling or compilation, when a code generator traverses the AST and generates assembly code from it
assembling, when the compiler driver invokes an assembler program which turns the assembly text into binary object code and finally
linking or linkage, when the compiler driver invokes a linker to look up symbols in libraries, fill in missing addresses, etc.
So, strictly speaking, only the 3rd small step is the compilation; furthermore, using the GNU toolchain and make, people tend to call the first four steps (producing an object file from a .c source file) compilation as one.
More on all this here...
Compiling is generally thought of as turning source code into machine code, but not necessarily also linking the machine code to create a final executable. Building is a more general term to describe the whole process of creating the final executable. Building will involve both compiling and linking. If you aren't using the -c option, then the linking is done automatically, so this would be considered building, but compiling was also part of that process.
You may find the terms being used a bit loosely though.
When you compile a program, the compiler just checks if the file has any compile-time errors (such as syntax and semantic errors) in it. When you "build" the program, the compiler first checks for any errors in it, and then converts the source code into machine code (actual compilation) and creates an executable file in the process.
For your second question, yes gcc -o helloworld helloworld.c alone would be enough to "turn the source code into an executable".