How to compile to linux

How to compile to linux

How To Compile Software Packages On Linux

It’s something that new users must learn to do

One of the hardest adjustments for users who switch from Windows to Linux is the idea that not every bit of software you want to install is ready-made for you. Unlike Windows users, who (in most cases) get software pre-packaged in an EXE installer or ZIP file, Linux users often have to compile their own software packages.

If you want to know how to compile software packages on Linux, you’ll need to follow a few steps. You’ll need to download the source code, run the configure command, install any required dependency packages, then run the make command to begin compiling your package. Here’s how to do all of this on a Linux-based operating system.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

Downloading Source Files

Before you begin building your new software packages, you need the source code. This could be from a package that you’ve developed yourself, in which case you should have access to the source code already.

It’s more likely, however, that you’re attempting to compile a software package on Linux from another developer. Popular code sharing sites like Github allow you to view and download the source code for packages, which you can then compile. You can use GIT, the popular version control system, to download the source files to your PC.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

Once you have the source code available and extracted on your Linux PC, you can move to the next stage of preparation before you begin compiling your package.

Installing Build-Essential On Linux

The tools and software contained in the build-essential package are required for any kind of software compiling on Linux operating systems, regardless of the programming language of your source code.

As an essential package, build-essential (or similarly named packages) should be available in the software repository for your Linux distribution. The equivalent of build-essential on Arch Linux-based distributions is called base-devel, which includes many of the same tools.

The installation instructions for build-essential will also vary, depending on your Linux distribution. For example, on Ubuntu and Debian-based operating systems, you can install build-essential by opening a terminal window and typing sudo apt install build-essential.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

Installing build-essential will also install its dependencies, like the g++ package. Once this process is complete, you can move onto configuring your Linux source package before compiling it.

Run The Configure Command

The source code for major packages usually contains a configure script. Running this script will check your Linux distribution for the essential packages that your source code needs to be able to compile correctly.

To run the configure script, enter the folder for your extracted source code using the cd command. From there, type ./configure into the terminal, pressing enter to run it.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

If the configure script detects a missing package, it will inform you of what to do at the end of the script. For example, before compiling the VLC media player, the configure script shown running above has detected that the Lua programming language is not installed.

In some cases, you can still compile and configure software packages, even if the configure script has detected a missing package or feature. The configure script for VLC (shown above) has offered a solution to the missing Lua programming language packages by suggesting you run it again with the –disable-lua flag to bypass it.

You’ll need to install any missing packages that the configure script has detected or use any suggested disable flags to bypass these errors before you can continue.

If the configure script has completed with no (or only minor) errors, the configured makefile for your package will be created. This creates the instructions to compile your package, allowing you to move to the final software compiling stage.

Install Missing Dependency Packages

The configure script helpfully identifies any packages that your Linux distribution requires to be able to compile and install your new software package correctly.

These may be clearly identified by your configure script error message or further back through the process of running that script. If the error message hasn’t made it clear, scroll back through your terminal history to try and identify the missing package.

Once you know what the missing package is, use the package installer for your Linux distribution to install it. For instance, on Ubuntu and Debian-based operating systems, running sudo apt install package-name will install a package.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

Installing any missing dependencies is the final stage you need to complete before you can begin compiling and installing your new software package. Once you’ve completed this process, you’re ready to begin compiling.

How to Compile on Linux

The build-essential package contains make, the automatic tool used to begin compiling your source code into software that you can run on your PC. It uses the makefile file, configured and created by the earlier configure command, which contains the specific instructions needed to compile your package.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

To begin compiling your source code, open a terminal and use the cd command to enter the correct folder. When you’re ready, type make to begin compiling your package.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

This will take a little bit of time to complete, depending on the size of the package and your available system resources. If no errors appear after your software package has been compiled, you can then install your package.

To do this, type sudo make install in the terminal. The package will be installed on your Linux PC, ready for you to open and use like any other software.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

Installing New Software On Linux

Knowing how to compile software packages on Linux can help you install less commonly-known software. Major operating systems like Ubuntu and Devian have large software repositories available to users, so if you don’t want to compile your software, try finding and installing new software using the package manager instead.

If you’re moving from Windows, you can also install Windows software on Linux to keep using your favorite Windows-only apps.

Ben Stockton is a freelance technology writer based in the United Kingdom. In a past life, Ben was a college lecturer in the UK, training teens and adults. Since leaving the classroom, Ben has taken his teaching experience and applied it to writing tech how-to guides and tutorials, specialising in Linux, Windows, and Android. He has a degree in History and a postgraduate qualification in Computing. Read Ben’s Full Bio

How To Compile And Run C, C++ Programs In Linux

This brief tutorial explains how to compile and run C/C++ programs in GNU/Linux operating system. If you’re a student or a new Linux user coming from Microsoft platform, then you might be wondering how to run the C or C++ programs in a Linux distribution. Because, compiling and running code in Linux platforms is little bit different than Windows. Let us get started, shall we?

Setup Development Environment

As you may already know, we need to install the necessary tools and compilers to run the code, right? Yes! Refer the following guide to install all development tools in your Linux box.

The development tools includes all necessary applications, such as GNU GCC C/C++ compilers, make, debuggers, man pages and others which are needed to compile and build new software, packages etc.

Also, there is a script named ‘manji’ that helps you to setup a complete environment in Ubuntu-based systems.

After installing the necessary development tools, verify them using any one of the following commands:

These commands will display the installation path and version of gcc compiler.

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

Compile And Run C, C++ Programs In Linux

First, let us see how to compile and run a simple program written in C language.

Compile And Run C Programs

Write your code/program in your favorite CLI/GUI editor.

I am going to write my C program using nano editor.

Note: You need to use extension .c for C programs or .cpp for C++ programs.

Copy/paste the following code:

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

Press Ctrl+O and Ctrl+X to save and quit the file.

To compile the program, run:

If there are any syntax or semantic errors in your code/program, they will be displayed. You need to fix them first to proceed further. If there is no error then the compiler will successfully generate an executable file named ostechnix in the current working directory.

Finally, execute the program using command:

You will see an output like below:

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

To compile multiple source files (Eg. source1 and source2) into executable, run:

To allow warnings, debug symbols in the output:

To compile the source code into Assembler instructions:

To compile the source code without linking:

The above command will create a executable called source.o.

If your program contains math functions:

For more details, refer the man pages.

Compile And Run C++ Programs

Write your C++ program in any editor of your choice and save it with extension .cpp.

Here is a simple C++ program.

To compile this C++ program in Linux, simply run:

If there were no errors, you can run this C++ program under Linux using command:

Sample output would be:

Alternatively, we can compile the above C++ program using «make» command like below.

How to compile a c++ program in Linux?

I made a file hi.cpp and I wrote the command given below:

then I ran it in my RHEL 6 machine with the following command

and I got some errors which are as follows:

What do these errors denote? My code is correct then why am I getting errors?

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

8 Answers 8

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

Switch to Trending sort

As the other answers say, use g++ instead of gcc.

Or use make: make hi

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

You have to use g++ (as mentioned in other answers). On top of that you can think of providing some good options available at command line (which helps you avoid making ill formed code):

gcc is only for C

For a simple hello-world project, calling the compiler directly with g++ command or creating a make file are good options as already answered:

For serious projects, however, the usage of a project manager is required. At the time I write this answer, the most used and open-source is cmake (an alternative could be QT qmake ).

Following is a simple CMake example:

Create a file CMakeLists.txt (the name is important) together with your source hi.cpp

Then compile and run as:

This allows the project to scale easily with libraries, sources, unit-tests, and much more. It also makes most IDEs to understand the project properly (Most IDEs accept CMake natively, like kdevelop, qtCreator, etc..)

How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

You could also generate Visual-Studio or XCode projects from CMake, in case you decide to port the software to other platforms in the future.

Ubuntu Documentation

Compiling things on Ubuntu the Easy Way

Let’s say you are a moderately experienced Linux user, and you want to install an application off the Internet but it doesn’t have a nice package that works on your system. (If it does have a package, install it following the instructions on InstallingSoftware.)

A lot of users, even quite experienced ones, have issues with going from the tarball to the installed program because they just do not know the fairly easy steps required to get the job done. But it’s only easy if you already know how to do it! So, here’s a quick guide about how to install stuff from developer sites.

Step 1: Prep your system for building packages

By default, Ubuntu does not come with the tools required. You need to install the package build-essential for making the package and checkinstall for putting it into your package manager. These can be found on the install CD or in the repositories, searching in Synaptic Package Manager or the command-line apt-get:

And since you may want to get code from some projects with no released version, you should install appropriate version management software.

You should then build a common directory for yourself where you’ll be building these packages. We recommend creating /usr/local/src, but really you can put it anywhere you want. Make sure this directory is writable by your primary user account, by running

and, just to be safe

After you’ve done this, you’re set up to start getting the programs you need.

Step 2: Getting the software you want

and for bz2 the similar command:

In the rare case of getting a program from a cvs or subversion repository, the developers will generally provide instructions on how to do this on their website. If you already installed the packages listed on Step 1, you just need to change to your /usr/local/src directory (cd /usr/local/src) and run the commands that are listed. The procedure will vary from program to program, so I can’t help you here, but with the given packages the instructions they provide should work smoothly.

from within the downloaded files top directory. This command relies on automake and autoconf programs and will automatically build the configuration files and run the ./configure command. After this step you can resume the later directions by running the command

Step 3: Resolving Dependencies.

One nice thing about modern Linux distributions is they take care of dependencies for the user. That is to say, if you want to install a program, the apt program will make sure it installs all needed libraries and other dependent programs so installing a program is never more difficult than just specifying what you need and it does the rest. Unfortunately with tarballs this is not the case, and you’ll have to do it manually. It’s this stage that trips up even some fairly experienced users who often give up in frustration for not being able to figure out what they need to get.

    You probably want to read about the possibilities and limitations of auto-apt first, which will attempt to take care of dependency issues automatically. The following instructions are for fulfilling dependencies manually:

    To prepare, install the package apt-file, and then run sudo apt-file update. This will download a list of all the available packages and all of the files those packages contain, which as you might expect can be a very large list. It will not provide any feedback while it loads, so just wait.

    The apt-file program has some interesting functions, the two most useful are apt-file search which searches for a particular file name, and apt-file list which lists all the files in a given package. (Two explanations: 1 2)

    To check the dependencies of your program, change into the directory you created in step two (cd /usr/local/src). Extracting the tarball or downloading from cvs/subversion will have made a sub-directory under /usr/local/src that contains the source code. This newly-created directory will contain a file called «configure», which is a script to make sure that the program can be compiled on your computer. To run it, run the command ./configure This command will check to see if you’ve got all the programs needed to install the program — in most cases you will not, and it will error out with a message about needing a program.

      If this happens, the last line of output will be something like

      But right above that it will list a filename that it cannot find (often a filename ending in «.pc», for instance). What you need to do then is to run

      which will tell you which Ubuntu package the missing file is in. You can then simply install the package using

      Then try running ./configure again, and see if it works. If you get to a bunch of text that finishes with config.status: creating Makefile followed by no obvious error messages, you’re ready for the next steps.

      Step 4: Build and install.

      If you got this far, you’ve done the hardest part already. Now all you need to do is run the command

      which does the actual building (compiling) of the program.

        If it’s a large program or if you’ve got a very slow computer, go and get a cup of coffee or something. If you have a multi-core processor you can also set the variable CONCURRENCY_LEVEL to the number of processors/cores you have to speed things up a little.

        When its done, install the program. You probably want to use

        which puts the program in the package manager for clean, easy removal later. This replaces the old sudo make install command. See the complete documentation at CheckInstall.

        Note: If checkinstall fails you may need to run the command like

        Which should allow the install to complete successfully. Bugs: 78455 & 599163

        Then the final stage of the installation will run. It shouldn’t take long. When finished, if you used checkinstall, the program will appear in Synaptic Package Manager. If you used sudo make install, your application will be installed to /usr/local/bin and you should be able to run it from there without problems.

          If this all seems way too hard for you, don’t fret. You’re using Ubuntu after all, and it has all of the programs that you actually need to get your work done already packaged for you. If there isn’t a package out there, the odds are that you really don’t need the program and within a few months someone will have packaged it for you. The only programs you actually need to build and compile like this are programs that are new and perhaps not yet stable or ready for your desktop. If you think this procedure is too hard, well maybe you ought to reconsider why you want to do this and just wait a few months for the next stable release. But it can be a good learning experience for you.

          If your desired package is quite important and you think it deserves to be in Ubuntu properly, perhaps contact the Masters of the Universe and see if they can do the hard work for you — if they package something, anyone can install it without having to go through this procedure. But if you can get through all this, you’re well on your way to becoming an expert Linux user — you’d be surprised how easy all this seems after you’ve done it just a few times. Good luck!

            Easy meaning «easier than tearing your hair out and then screaming about how much Linux sucks while running around the room». Not actually easy.

            Attachments

            The process described in this page can be performed without typing terminal commands, using the attached application. Download the package kludge_tarball_installer_v0.10.tar, extract it to your user folder, and see the README file.

            Links

            Comments

            For a more advanced yet not so easy Howto, have a look at CompilingSoftware.

              *Would be nice to see an example tar ball included in the post. Would make it easy for someone to test the steps.

              CompilingEasyHowTo (последним исправлял пользователь knome 2013-12-13 21:04:47)

              The material on this wiki is available under a free license, see Copyright / License for details
              You can contribute to this wiki, see Wiki Guide for details

              Compile C Program in Linux Using GCC

              In this article, I will show you how to install GCC and compile C programs in Linux using GCC. I will use Debian 9 Stretch for the demonstration. But I will show you how to install GCC on wide variety of Linux distributions. Let’s get started.

              Installing GCC on Ubuntu and Debian GNU/Linux:

              On Ubuntu and Debian GNU/Linux distributions, GCC is really easy to install as all the required packages are available in the official package repository of Ubuntu and Debian. There is a meta package called build-essential, which installs everything you need in order to compile C and C++ programs on Ubuntu and Debian GNU/Linux distribution.

              First, update the APT package repository cache with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              The APT package repository cache should be updated.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now install build-essential with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now press y and then press to continue.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              GCC should be installed.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now you can check whether GCC is working with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Installing GCC on Linux Mint:

              You can install GCC on Linux Mint the same way as in Ubuntu/Debian as shown in the earlier section of this article.

              Installing GCC on CentOS 7 and Fedora:

              On CentOS 7 and Fedora, GCC is easier to install as well. The required packages are available in the official package repository of CentOS 7 and Fedora. You can install the Development Tools group to install all the required packages to compile C and C++ programs on CentOS 7 and Fedora.

              First, update the YUM database with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              YUM database should be updated.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now install Development Tools group packages with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now press y and then press to continue.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              GCC should be installed.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now you can check whether GCC is working with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Installing GCC on Arch Linux:

              You can install GCC on Arch Linux too. All the required packages are available in the Arch package repository. Arch also has a meta package base-devel, which you can install to get all the required tools needed to compile C and C++ programs on Arch Linux.

              First, update the Pacman database with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Pacman database should be updated. In my case, it was already up to date.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now install base-devel package with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now press to select all unless you want to install very specific set of packages.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              GCC should be installed.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now check whether GCC is working with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Writing Your First C Program:

              Now let’s write a very simple C program, which we will compile in the next section of this article below using GCC C compiler.

              First, create a project directory (I am going to call it hello) with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now navigate to the newly created directory with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now create a new C source file (I am going to call it main.c) here with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now open the file with any text editor (such as vim, nano, gedit, kate etc) of your choice.

              To open the file with nano, run the following command:

              To open the file with vim, run the following command:

              To open the file with Gedit, run the following command:

              To open the file with Kate, run the following command:

              I am going to use Gedit text editor in this article.

              Now type in the following lines and save the file.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Here, line 1 includes the stdio.h header file. It has function definition for the printf() function I used on line 4.

              Every C program must have a main() function. It is the function that will get called when you run a C program. If you don’t write a main() function, you can’t run the C program. So I wrote a main() function in line 3line 7.

              Inside the main() function, I called printf() library function in line 4 to print some text to the screen.

              Finally, in line 6, I returned 0 from the program. On Linux world, when a program returns 0, it means the program ran successfully. You can return any integer you like but there are some Linux specific rules on what return value means what.

              In the next section, I will show you how to compile the C program with GCC and run it.

              Compiling and Running C Programs with GCC:

              The command to compile a C source file with GCC is:

              NOTE: Here, SOURCE_FILES is a whitespace separated list of C source files. The compiled executable file will be saved as OUTPUT_BINARY in your current working directory.

              In our case, the main.c source file doesn’t depend on other C source file, so we can compile it with the following command:

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              The source file main.c should be compiled and hello executable file should be created as you can see in the screenshot below.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              Now, you can run the hello executable binary file as follows:

              As you can see, the correct output is printed on the screen.

              How to compile to linux. Смотреть фото How to compile to linux. Смотреть картинку How to compile to linux. Картинка про How to compile to linux. Фото How to compile to linux

              So that’s basically how you use GCC to compile C programs on Linux. Thanks for reading this article.

              Источники информации:

              Добавить комментарий

              Ваш адрес email не будет опубликован. Обязательные поля помечены *