LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2009, 07:39 AM   #1
skazhy
LQ Newbie
 
Registered: Nov 2008
Location: Latvia
Distribution: arch
Posts: 23

Rep: Reputation: 15
linking and compiling multiple c++ files into one


Hi!

I have 3 c++ files, classdef.h (header file with class definition), methods.cpp (class methods) and program.cpp - the program itself. Both .cpp files have
Code:
 #include "classdef.h"
in files.

How I can link the files together and compile them in one executable program? I am using Geany IDE for coding.

Thanks in advance,

-skazhy
 
Old 12-17-2009, 09:40 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I don't know about Geany, but with gcc:

Code:
g++ -o program program.cpp methods.cpp
The "-o program" flag causes the executable to be named "program" instead of the default "a.out".

g++ will automatically include the necessary .h files.

Don't know if this will help.
 
Old 12-17-2009, 09:55 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
The g++ program is a supervisor that invokes the compiler and linker for you.

The operation
Code:
g++ -o program program.cpp methods.cpp
tells it to independently:
compile program.cpp
compile methods.cpp
link the two results together plus startup code and default libraries.

With compilers on many other platforms, including two cpp files in one compile command would tell the compiler to compile the concatenated source code, rather than compile separately. That is rarely what you want, so on such systems, you would issue the two separate compile commands followed by the link command.

Even with GCC, you may prefer to give compile and link commands separately:
Code:
g++ -c program.cpp
g++ -c methods.cpp
g++ -o program program.o methods.o
Most IDEs manage all that for you (generate that sequence of separate g++ commands, or more commonly generate a makefile then issue a make command to issue the right sequence of g++ commands). I have no idea what Geany does for that.

If you want Geany to generate the g++ commands for you, read its documentation and/or put "Geany" in the topic of your thread so someone who knows it is more likely to respond.

Last edited by johnsfine; 12-17-2009 at 10:01 AM.
 
Old 12-17-2009, 10:08 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by johnsfine View Post
Even with GCC, you may prefer to give compile and link commands separately:
Code:
g++ -c program.cpp
g++ -c methods.cpp
g++ -o program program.o methods.o
I often do it that way, it helps with debugging.
 
Old 12-18-2009, 03:43 AM   #5
skazhy
LQ Newbie
 
Registered: Nov 2008
Location: Latvia
Distribution: arch
Posts: 23

Original Poster
Rep: Reputation: 15
thank you very much guys, this info was very helpful
 
  


Reply

Tags
c++


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
linking multiple files kernel module programming charlotteix9 Programming 1 02-17-2009 12:28 PM
Help compiling multiple source files with gcc. thethinker Programming 3 07-21-2007 10:48 PM
Compiling Multiple Files, g++ error. Please Help jamesjoy Programming 9 10-12-2005 09:43 AM
compiling linking help blackzone Programming 1 02-13-2005 10:40 PM
compiling and linking boku Programming 5 12-25-2002 09:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration