473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

multiple files

i have a .cpp file and i want to have my class and member functions in a
seperate file. if i put the class heading file in a .h and the member
functions for the class in a .cpp. how do i compile the 2 .cpp files eg 1
with class stuff and other main(). i know i include the .h file in the .cpp
class member file but i cant compile the project without errors.

how do i set-up and compile a program with class .h class .cpp, and main()

Jul 23 '05 #1
2 1241
john townsley wrote:
how do i set-up and compile a program with class .h class .cpp, and

main()

The details of the compilation are actually environment specific and
thus cannot really be answered here. However, if you use a command
line interface, the process is quite similar on most platforms and
even the compiler flags are identical.

Assume you have a simple setup with three files: one header and two
sources:

// file: foo.h
#if !defined(FOO_H)
#define FOO_H

struct foo { void bar(); };

#endif

// file: foo.cpp
#include "foo.h"
#include <iostream>
void foo::bar() { st::cout << "hello, world\n"; }

// file: main.cpp
#include "foo.h"
int main() { foo().bar(); }

The easiest approach to compile these two files and link the result is
to
simply give all files as command line arguments to the compiler (I will
use "CXX" as the name of the compiler executable although this is not
really the name of any C++ compiler I now):

CXX -o tst foo.cpp main.cpp

Since compiling all files may be rather time consuming, you normally
compile the C++ files to object files and link them in a separate step:

CXX -c foo.cpp // use default name for object file
CXX -o main.o -c main.cpp // explicitly name the object file
CXX -o tst foo.o main.o // link the resulting object files

This is unwieldy once you really have a lot of files in which case you
would start to lump some of them together into libraries and/or shared
libraries. However, this becomes even more environment specific than
the above: although I would guess that the above actually works on all
systems, I'm pretty sure the "preferred" options are actually different
and some of "usual" options are not even documented. Thus, you need to
look up the details in your environment's documentation.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2

"john townsley" <jo**********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
i have a .cpp file and i want to have my class and member functions in a
seperate file. if i put the class heading file in a .h and the member
functions for the class in a .cpp. how do i compile the 2 .cpp files eg 1
with class stuff and other main(). i know i include the .h file in the
.cpp
class member file but i cant compile the project without errors.

how do i set-up and compile a program with class .h class .cpp, and main()


What compiler? For most IDE's, you add all the .cpp files to your project.
They will then get compiled when you build the project. But for specifics
on a given compiler, ask in a newsgroupd dedicated to that compiler.

-Howard

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: x. zhang | last post by:
Hi Guys, We know that we can use <input type=file ...> to upload one file per time to the server. My question is if there are some way to upload multiple files per time to the server. (Of...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
4
by: ziycon | last post by:
I have a string passed in and I want to check validate it to make sure that multiple values don't exist at the start, the below would only check the entire string for the value??...
5
by: merkam | last post by:
hi, i need a script that: step1: multiple n*1200= m n=0 or 1 or 2 ... step2: sum m+10+20 i have this script i don't now how to assign span with id="five" to value in checkbox plz help me <%@...
1
by: dorandoran | last post by:
I created an MSI package and it works great. However, the source code aren't compiled. They show up row in the test web server. This is No No in our environment. Are there any steps that I am missing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.