Bill G.
Bill G.

Reputation: 151

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:

Upvotes: 15

Views: 1593

Answers (2)

Prime
Prime

Reputation: 4251

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.

Upvotes: 10

marinara
marinara

Reputation: 538

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

Upvotes: 4

Related Questions