Include in Xcode

Atomical
Unregistered
 
Post: #1
At first I tried putting all the includes exclusively in main.cpp. After that didn't work I put all includes that were relevent in each .cpp file. Then it told me I was redeclaring functions. What is the proper way to include files?
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #2
Are these standard includes or your own headers? Do you have a code snippet you could show?

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
you need to put header guards around your custom headers.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #4
Say you have 3 files, main.cpp, myclass.cpp and myclass.h.

Obviously, myclass.h contains the declaration of your class and myclass.cpp contains the definitions of functions of that class. main.cpp uses the class you've put in the files myclass.h/.cpp. Now, to set this up so it works properly...

1. Add main.cpp and myclass.cpp to the project source.
2. Add #include "myclass.h" to the top of myclass.cpp and main.cpp
3. Add
Code:
#ifndef _MYCLASS_H
#define _MYCLASS_H
...all the code in myclass.h here...
#endif
4. Run it
Quote this message in a reply
Atomical
Unregistered
 
Post: #5
http://www.cs.niu.edu/~duffin/csci241/c2...ode83.html

So basically I should be doing this? Is there an easier way? That seems like an aweful amount of work that the compiler could be doing.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Names beginning with two underscores or an underscore and a capital letter are reserved for the "implementation" -- language/standard library/OS. Don't use them.

I recommend using the exact name of the file, including capitalization, with illegal characters replaced by underscores as the header guard. eg, SomethingCool.h would begin with #ifndef SomethingCool_h
Quote this message in a reply
sheijk
Unregistered
 
Post: #7
I recommend adding the creation date or another unique number to the header guard's name. Otherwise you might run into troubles if you've got two files with the same name in different directories which most likely will occur with bigger projects. (Two classes with same name in different namespaces etc)

Code:
#ifndef MYHEADER_H_2005_06_06
#define MYHEADER_H_2005_06_06
...
#endif
Quote this message in a reply
Moderator
Posts: 437
Joined: 2002.09
Post: #8
These header guards are the most universal way to solve the problem even though it feels like a lot of work. It becomes automatic to you very quickly.

If you are coding in Cocoa you can use #import instead of #include, which will only bring in a file if it hasn't been included already. GCC at one point let you use it with regular C, but Google tells me that there was talk of removing it again (presumably because it's not in the C standard.)

Some compilers support putting
#pragma once
at the top of a header file, which again causes it to be included at most once. Again, it's not standard, looks like GCC depreciated it but may have brought it back. You'll have to find out the status of that. Microsoft's C++ compiler supports it too. But it's not technically something you should rely on compilers supporting.

The header guards use only very basic C language constructs and will always work.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #9
It's not a lot of work. Every time you create a new header, put the header guards in before you type anything else.

It will take all of two seconds.

Wade
Quote this message in a reply
Atomical
Unregistered
 
Post: #10
It worked temporarily. Is this related to the header guards?

ld: multiple definitions of symbol _Z7striposSsSsi.eh
ld: multiple definitions of symbol fetch_urls(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)
ld: multiple definitions of symbol stripos(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)
ld: multiple definitions of symbol auth(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_ostream<char, std::char_traits<char> >&)
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #11
functions defined in a header maybe?

certainly you've got the same code in multiple .cpp files, possibly via an include.
Quote this message in a reply
Atomical
Unregistered
 
Post: #12
There aren't any functions defined in the header.. I removed them after I got those errors. I've got header guards in all cpp files except main.cpp.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #13
header guards go in .h files, not .cpp files...
Quote this message in a reply
Atomical
Unregistered
 
Post: #14
I include my .cpp files from my header files then? What I have been doing is putting my header guards in .cpp and then including .h files from cpp and then including my .cpp files from main.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #15
gah!

never include a .cpp file
put header guards in header files
include header files from header files or .cpp files as necessary
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  C++ include files krazygluon 18 8,689 Apr 23, 2007 07:46 PM
Last Post: OneSadCookie