Author Topic: How To Use Multiple .cpp Files?  (Read 14376 times)

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
How To Use Multiple .cpp Files?
« on: August 19, 2007, 08:07:56 pm »
I'm starting to hopefully code a game for the Z, but I need to put stuff not only in multiple .hpp files but also in .cpp files. Does anybody have a good link for how to do this? My horrible searching skills failed to turn anything up.

Also, is there a good tutorial for using the autotools/makefiles? Those seem to be important to using multiple source files as well.

Thanks.
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

koan

  • Sr. Member
  • ****
  • Posts: 370
    • View Profile
    • http://www.lyndonhill.com
How To Use Multiple .cpp Files?
« Reply #1 on: August 19, 2007, 08:14:41 pm »
Quote
I'm starting to hopefully code a game for the Z, but I need to put stuff not only in multiple .hpp files but also in .cpp files. Does anybody have a good link for how to do this? My horrible searching skills failed to turn anything up.

Also, is there a good tutorial for using the autotools/makefiles? Those seem to be important to using multiple source files as well.

Thanks.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=166451\"][{POST_SNAPBACK}][/a][/div]

What is an hpp file ?

I assume you mean a C/C++ header file; most people use .h and for C++ source files they use .cc or .cpp. Some people even use .C. I prefer .cc.

How about arm-linux-g++ file1.cpp file2.cpp -o myprogram ?

You can do it all using GNU make. I have the O'Reilly book; I've found it useful.

I don't know about the dev system for pdaXrom. Do they use tmake/qmake ?
Zocalo Feed Reader : Thai on Zaurus : Dictionaries for zbedic : Sharp ROM package feed
HELUX Handheld Embedded Linux Blog
SL-C3200 Multiboot : SL-C750  Sharp ROM

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
How To Use Multiple .cpp Files?
« Reply #2 on: August 19, 2007, 08:58:05 pm »
Thanks for the quick reply. How do I make function calls to the other .cpp files then? I don't understand how it works if you aren't including them.

I use .hpp instead of .h because editors with syntax highlighting highlight differently for C than they do for C++, which makes it odd to read. With .hpp, they use the C++ highlighting, making it more consistant.
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

Meanie

  • Hero Member
  • *****
  • Posts: 2803
    • View Profile
    • http://www.users.on.net/~hluc/myZaurus/
How To Use Multiple .cpp Files?
« Reply #3 on: August 19, 2007, 09:18:36 pm »
Quote
Thanks for the quick reply. How do I make function calls to the other .cpp files then? I don't understand how it works if you aren't including them.

I use .hpp instead of .h because editors with syntax highlighting highlight differently for C than they do for C++, which makes it odd to read. With .hpp, they use the C++ highlighting, making it more consistant.
[div align=\"right\"][a href=\"index.php?act=findpost&pid=166456\"][{POST_SNAPBACK}][/a][/div]

you are supposed to have function definitions for each of your methods and include those in header files so that other source files that need to use them can then simply include those headers.

hpp files are a newer standard for cpp header files and usually are used to define cpp templates. you can even mix cpp and c objects to some extend by using the extern c definitions...

tmake/qmake are for generating QTE / QT makefiles. if you are trying to generate makefiles for QT under pdaXrom you would also be using qmake, but for general c/c++ code, you would use automake to generate the build templates and then run configure and make to actually compile and build.
SL-C3000 - pdaXii13 build5.4.9 (based on pdaXrom beta3) / SL-C3100 - Sharp ROM 1.02 JP (heavily customised)
Netgear MA701 CF, SanDisk ConnectPlus CF, Socket Bluetooth CF, 4GB Kingston CF,  4GB pqi SD, 4GB ChoiceOnly SD, 2GB SanDisk SD USB Plus, 1GB SanDisk USB Plus, 1GB Transcend SD, 2GB SanDisk MicroSD with SD adaptor, Piel Frama Leather Case, GoldX 5-in-1 USB cable, USB hub, USB mouse, USB keyboard, USB ethernet, USB HDD, many other USB accessories...
(Zaurus SL-C3000 owner since March 14. 2005, Zaurus SL-C3100 owner since September 21. 2005)
http://members.iinet.net.au/~wyso/myZaurus - zBook3K

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
How To Use Multiple .cpp Files?
« Reply #4 on: August 19, 2007, 10:01:13 pm »
Ah, that mostly clears it up. A couple of follow-up questions:

1. So I make a prototype function in the header ( void myfunction(int myint); ), then have the complete function in the .cpp file? (Yes, as it works)

2. How do I use automake if I don't have a configure.in or configure.ac file?

Thanks.

EDIT: I'm getting errors when I try to compile with the files like they are:

Code: [Select]
Fish LOZR # g++ lozr.cpp sprite.cpp screen.cpp input.cpp  -o lozr -lSDL -lSDL_image
lozr.cpp: In function 'int main(int, char**)':
lozr.cpp:42: warning: deprecated conversion from string constant to 'char*'
/tmp/ccXyIXFl.o:(.bss+0x0): multiple definition of `spriteMain'
/tmp/cc2NlQAW.o:(.bss+0x20): first defined here
/tmp/ccXyIXFl.o:(.bss+0x2c): multiple definition of `surfScreen'
/tmp/cc2NlQAW.o:(.bss+0x4c): first defined here
/tmp/ccEAhPKQ.o:(.bss+0x0): multiple definition of `surfScreen'
/tmp/cc2NlQAW.o:(.bss+0x4c): first defined here
/tmp/ccxIcIsw.o:(.bss+0x0): multiple definition of `myEvent'
/tmp/cc2NlQAW.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Fish LOZR #

What's wrong?
« Last Edit: August 19, 2007, 10:44:20 pm by Capn_Fish »
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

danr

  • Full Member
  • ***
  • Posts: 138
    • View Profile
    • http://
How To Use Multiple .cpp Files?
« Reply #5 on: August 20, 2007, 04:48:34 am »
Quote
EDIT: I'm getting errors when I try to compile with the files like they are:

Code: [Select]
Fish LOZR # g++ lozr.cpp sprite.cpp screen.cpp input.cpp  -o lozr -lSDL -lSDL_image
lozr.cpp: In function 'int main(int, char**)':
lozr.cpp:42: warning: deprecated conversion from string constant to 'char*'
/tmp/ccXyIXFl.o:(.bss+0x0): multiple definition of `spriteMain'
/tmp/cc2NlQAW.o:(.bss+0x20): first defined here
/tmp/ccXyIXFl.o:(.bss+0x2c): multiple definition of `surfScreen'
/tmp/cc2NlQAW.o:(.bss+0x4c): first defined here
/tmp/ccEAhPKQ.o:(.bss+0x0): multiple definition of `surfScreen'
/tmp/cc2NlQAW.o:(.bss+0x4c): first defined here
/tmp/ccxIcIsw.o:(.bss+0x0): multiple definition of `myEvent'
/tmp/cc2NlQAW.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Fish LOZR #

What's wrong?
[div align=\"right\"][a href=\"index.php?act=findpost&pid=166459\"][{POST_SNAPBACK}][/a][/div]

If your .cpp files are #including the same header file, then the header file needs guards around it to stop it being included more than once.

So for example, if your header is called header.h, you would have the following in it:

Code: [Select]
#ifndef HEADER_H
#define HEADER_H

#endif

This would then ensure that the header file is included only once during compilation.

Dan
SL-C860 running Debian EABI on top of Angstrom 2.6 kernel

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
How To Use Multiple .cpp Files?
« Reply #6 on: August 20, 2007, 10:24:30 am »
I've done that, that's why I find this so odd (not to mention the fact that I've never tried to build an app this way).

Do I need to do it in the .cpp files too? It sounds wrong, but you never know.
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

Capn_Fish

  • Hero Member
  • *****
  • Posts: 2342
    • View Profile
    • http://
How To Use Multiple .cpp Files?
« Reply #7 on: August 20, 2007, 05:20:01 pm »
I figured it out. I needed to use "extern (variable)" in the header, then put the actual thing in the .cpp file.
SL-C750- pdaXrom beta 1 (mostly unused)
Current distro: Gentoo

koan

  • Sr. Member
  • ****
  • Posts: 370
    • View Profile
    • http://www.lyndonhill.com
How To Use Multiple .cpp Files?
« Reply #8 on: August 20, 2007, 10:25:50 pm »
Quote
hpp files are a newer standard for cpp header files and usually are used to define cpp templates. you can even mix cpp and c objects to some extend by using the extern c definitions...

Hi Meanie,

Is hpp really a new standard ? I never heard of it. Can you supply a link to where I can find more info ?

I always mixed C and C++ in header files with no problems.

thanks

koan
Zocalo Feed Reader : Thai on Zaurus : Dictionaries for zbedic : Sharp ROM package feed
HELUX Handheld Embedded Linux Blog
SL-C3200 Multiboot : SL-C750  Sharp ROM

Meanie

  • Hero Member
  • *****
  • Posts: 2803
    • View Profile
    • http://www.users.on.net/~hluc/myZaurus/
How To Use Multiple .cpp Files?
« Reply #9 on: August 20, 2007, 10:40:47 pm »
Quote
Quote
hpp files are a newer standard for cpp header files and usually are used to define cpp templates. you can even mix cpp and c objects to some extend by using the extern c definitions...

Hi Meanie,

Is hpp really a new standard ? I never heard of it. Can you supply a link to where I can find more info ?

I always mixed C and C++ in header files with no problems.

thanks

koan
[div align=\"right\"][{POST_SNAPBACK}][/a][/div]

[a href=\"http://filext.com/file-extension/HPP]http://filext.com/file-extension/HPP[/url]

well, the hpp standard for c++ headers was introduced after c++ came to which naturally was after the c standard so in effect, the hpp header convention for c++ headers and h for c headers was newer. however, it was not widely adopted and because of non consensus reasons caused by many programmers that still mix and match c and c++ and many people still use .h files for cpp. hpp headers are used for pure c++ code only and since many cpp programs still have elements of c in them and most people were programming in a hybrid c/c++ mode anyway, the result was hpp was not used that much so mainly c++ extensions which are pure c++ will use hpp file extensions.
anyway, its just a convention which makes it easier to detect c++ headers. some people also use .hh instead of .hpp so its not really a standard but more like a convention, but let's not fight about wording symantics. hpp is a commonly used standard convention but not an enforced standard...
« Last Edit: August 20, 2007, 10:49:32 pm by Meanie »
SL-C3000 - pdaXii13 build5.4.9 (based on pdaXrom beta3) / SL-C3100 - Sharp ROM 1.02 JP (heavily customised)
Netgear MA701 CF, SanDisk ConnectPlus CF, Socket Bluetooth CF, 4GB Kingston CF,  4GB pqi SD, 4GB ChoiceOnly SD, 2GB SanDisk SD USB Plus, 1GB SanDisk USB Plus, 1GB Transcend SD, 2GB SanDisk MicroSD with SD adaptor, Piel Frama Leather Case, GoldX 5-in-1 USB cable, USB hub, USB mouse, USB keyboard, USB ethernet, USB HDD, many other USB accessories...
(Zaurus SL-C3000 owner since March 14. 2005, Zaurus SL-C3100 owner since September 21. 2005)
http://members.iinet.net.au/~wyso/myZaurus - zBook3K

koan

  • Sr. Member
  • ****
  • Posts: 370
    • View Profile
    • http://www.lyndonhill.com
How To Use Multiple .cpp Files?
« Reply #10 on: August 21, 2007, 02:51:11 am »
Quote
http://filext.com/file-extension/HPP

OK, thanks.

I just thought that as you were saying it's a standard I'd missed something.

To be perfectly straightforward though, I've never met source code that used .hpp

koan
Zocalo Feed Reader : Thai on Zaurus : Dictionaries for zbedic : Sharp ROM package feed
HELUX Handheld Embedded Linux Blog
SL-C3200 Multiboot : SL-C750  Sharp ROM