Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Introduction to use of GCC / make for Visual Studio users

I've developed a tool in C++, using Visual Studio 2010, which I'd like to deploy on Linux systems as well. The code itself is programmed entirely platform-independent, using only the STL and the standard library.

Now my problem is: I don't have experience with Linux.

I have, however, tried to get some other programs I wrote to compile using GCC, and the results were a truckload of errors being thrown at me, which took me 3 hours to resolve - the horrors!

Noting from this experience I think that the same is about to happen, just a lot worse, if I try to port my current project to GCC.

My questions are:

  • What does a Visual Studio user need to know to successfully get their program running on Linux? (do I need to learn make?)

  • Do you know of a good source which covers not the topic of GCC / Linux programming as a whole, but specifically the problem of switching from a Visual Studio environment?

like image 594
Bill G. Avatar asked Jul 23 '11 22:07

Bill G.


People also ask

What is GCC in Visual Studio Code?

GCC stands for GNU Compiler Collection; GDB is the GNU debugger. After configuring VS Code, you will compile and debug a simple C++ program in VS Code. This tutorial does not teach you GCC, GDB, Ubuntu or the C++ language. For those subjects, there are many good resources available on the Web.

What is the compiler used in Visual Studio?

CMake, Clang, mingw, and more Use MSBuild with the Microsoft Visual C++ compiler or a 3rd party toolset like CMake with Clang or mingw to build and debug your code right in the IDE.

Does Visual Studio Code have GCC?

In this tutorial, you configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger from mingw-w64 to create programs that run on Windows. After configuring VS Code, you will compile and debug a simple Hello World program in VS Code.


2 Answers

I recommend skipping make altogether, its a rather old technology and you may face portability issues while using it. Instead, learn another build system like CMake http://www.cmake.org/ or SCons http://www.scons.org/

I use CMake myself and find it to be excellent. You write very simple build scripts (you can easily get started in an hour or two) and it generates the makefiles for you. The biggest advantage is that it can generate makefiles for almost any compiler or build system you could want. It can generate standard unix makefiles, Microsoft Visual C++ Projects, XCode Projects, Code::Blocks projects, even KDevelop and Eclipse CDT4 projects.

I haven't used SCons myself, but I do know that it actually builds your program for you and runs on python.

Getting started in Linux/Unix can really mean anything you want. Going from Visual Studio can mean going to Eclipse or another IDE, which is as simple as learning the new IDE, or it can mean going straight to the shell and forgetting you ever knew what an IDE looked like. My personal recommendation is to stick with the IDE- Eclipse is great as an industry standard and its very cross-platform (just get the CDT plugin).

On the topic of the GCC, you probably won't really be invoking it yourself very much if you're writing CMake scripts since CMake will generate the makefiles. The simplest command line arguments are:

g++ <source-files> -o <output-name> -I <another include directory> -l <library to link to>

as an example:

g++ helloworld.cpp -o world.out -I /usr/include -l mylib

To run an executable from the shell, navigate to the directory its in and type:

./world.out

Note that the default output when invoking g++ (i.e. g++ helloworld.cpp) is a.out.

And that's all you really need to know! The rest comes easily. You'll learn to love Unix, and I really recommend learning the shell even if you do go the path of the IDE. It can make your life alot easier.

EDIT: So to port your program to Linux and the GCC with CMake, here's what you would do:

  1. Get CMake
  2. Write the CMakeLists.txt file in your source directory (its the Makefile format CMake uses)
  3. Invoke CMake on the directory. CMake will parse the CMakeLists.txt file automatically and generate build scripts of your choice
  4. Build with whatever build system you used. If you're using standard Unix Makefiles, it'll mean just navigating to the build directory and typing make into the shell
  5. Your project will be built and youre done!

P.S: I never learned normal make, although it definitely has its uses. CMake found an eager user in me.

like image 100
Prime Avatar answered Oct 17 '22 10:10

Prime


i was going to say "man g++" but that manual is very long in lines.

just type

g++ main.cpp utility.cpp 

g++ will automatically compile and link main.cpp, utility.cpp into a file named a.out type ./a.out into command line to run the compiled code.

you won't need to learn make, but if you do, simple make scripts only take 4-5 lines of code. It's pretty easy to type in, but it's actually pretty different for a visual studio user, so it's completely non-friendly if you put bad code your Makefile.

about learning linux, there's a lot to learn. I can't even tell you where to start, but there's no secrets. Not like Microsoft products where you have to learn the workaround to make your code run.

oh and here's g++ info: http://homepages.gac.edu/~mc38/2001J/documentation/g++.html

like image 23
marinara Avatar answered Oct 17 '22 10:10

marinara