-2

My task: I have to compile some c/c++ project so that I will get an .exe file, also for the c/c++ files the script will generate some "object files".

From what I read I understood that i'll have to use the gcc bash compiler. I've looked for gcc references without succes online. I have some code lines:

gcc -c  $1.$2 -o obj/$1.o
      for i in $( seq $3)
      do
          gcc -c src/source$i.$2 -o obj/source$i.o
      done
      gcc $1.o obj/* -o $1

Is this any good? What exactly the "-c" and "-o" parameters do? Does anyone have any links for this or tutorials or so, please?

Marko
  • 407
  • 1
  • 7
  • 19
  • Executable file, so it says. Why is it necessary not to use the linker? – Marko Mar 28 '16 at 05:52
  • You have to run linker if you want to create executable – Ance Mar 28 '16 at 05:54
  • http://www.cyberciti.biz/faq/compiling-c-program-and-creating-executable-file/ Take a look at this – Ance Mar 28 '16 at 05:55
  • 1
    I did read it before too. I am confused. For example here (http://stackoverflow.com/questions/29090372/creating-a-bash-script-to-compile-a-c) it says that g++ filename.cpp -o anyname. Why g++ and not gcc ? – Marko Mar 28 '16 at 05:57
  • http://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc there is answer to that g++/gcc question. That example gcc file.cpp -o anyname creates executable named anyname if cpp file contains a main function. You can run it with command ./anyname – Ance Mar 28 '16 at 06:03
  • Got it, thank you, Ance! :) – Marko Mar 28 '16 at 06:09
  • Great, and No problem :) – Ance Mar 28 '16 at 06:11

1 Answers1

1

Linux has no particular strictness over file extensions therefore binary files don't normally end in .exe To create binaries from a C/C++ source file. You need a compiler such as GCC for .c and g++ for .cpp. These can be obtained from your Linux distro repository manager e.g in opensuse

sudo zypper install gcc g++

To compile a c source file named myfile.c to create a binary called myfile.

cd /path/to/myfile/
gcc -o myfile myfile.c

For a c++ source file.

$cd /path/to/myfile/
$g++ -o myfile myfile.cpp

For several C source files you can create appropriate header files and use ar command to create a static or shared library then link them e.g to create a static library from 2 c source files.

$cd /path/to/sources/
$gcc -c myfile1.c myfile2.c
$ar rs libmyfiles.a myfile1.o myfile2.o
$gcc -o main main.c -I/path/to/header-files/ -L/path/to/libmyfiles.a

The -c flag creates .o (object file) containing hexadecimal routines that are archived and indexed by the ar command into a static library called libmyfiles.a . shared libraries end in .so and need to be prduced with position-independent code (-lfpic).

However to link to an existing shared library e.g the pthread when you have used threading header file pthread.h you can link to the library as follows.

$gcc -o threadc thread.c -lpthread

To run the main program at the command line

$./mainprog

Where mainprog can be the name of the output binary file. You can also look into makefiles that ease the amount of work needed to compile large projects. There are also manual pages containing information about compiler flags

man gcc

or if you use KDE . you can insert this at the konqueror browser address bar for well formatted output.

man:gcc
harry_k
  • 56
  • 7