Include in Xcode

Moderator
Posts: 434
Joined: 2002.09
Post: #16
Atomical, now that you've wrestled with it, go back and re-read LongJumper's post, he's quite clear about what goes in the CPP and what goes in the H file. It may make more sense to you now.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Atomical
Unregistered
 
Post: #17
Here's an example of my .h and .cpp files

//The header file
/*
* strings.h
* server
*
* Created by adam on Tue Jul 05 2005.
* Copyright © 2005 __MyCompanyName__. All rights reserved.
*
*/
#ifndef STRINGSH
#define STRINGSH


#include "strings.cpp"
#endif

//The CPP file
/*
* strings.cpp
* server
*
* Created by adam on Tue Jul 05 2005.
* Copyright © 2005 __MyCompanyName__. All rights reserved.
*
*/
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int stripos (const string s , const string find, const int offset){
//tolower(find);
if ( offset >= (int)s.length()){
return false;
}
int i = offset;
int len;
int count;
int b;
int before;
len = find.length();
count = 0;
//i don't want to call this over and over
while ( s[i] ) {
before = i;
count = 0;
b = 0;

while (s[i] && find[b] && tolower(s[i]) == find[b] ) {
count++; i++; b++;
}
if ( count == len) {
//where it starts
return before;
}
i = before;
i++;
}
return 0;
}




I get these errors:
Native Build of Target "server"

CompileC build/server.build/server.build/Objects-normal/ppc/main.o main.cpp normal ppc c++ com.apple.compilers.gcc.3_3
mkdir -p "/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc"
cd "/Volumes/OWC Mercury Elite/proxyproject/server"
/usr/bin/gcc-3.3 -x c++ -arch ppc -pipe -Wno-trigraphs -fasm-blocks -fpascal-strings -Os -mtune=G4 -Wno-four-char-constants -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -fmessage-length=0 "-F/Volumes/OWC Mercury Elite/proxyproject/server/build" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/include" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/DerivedSources" "-Wp,-header-mapfile,/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/server.hmap" -c "/Volumes/OWC Mercury Elite/proxyproject/server/main.cpp" -o "/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o"
In file included from /Volumes/OWC Mercury Elite/proxyproject/server/html.h:12,
from /Volumes/OWC Mercury Elite/proxyproject/server/main.cpp:9:
/Volumes/OWC Mercury Elite/proxyproject/server/html.cpp: In function `void replace_relative(std::string&, const std::string*, std::ostream&)':
/Volumes/OWC Mercury Elite/proxyproject/server/html.cpp:91: warning: `int len' might be used uninitialized in this function
/Volumes/OWC Mercury Elite/proxyproject/server/main.cpp: In function `int main(int, char* const*)':
/Volumes/OWC Mercury Elite/proxyproject/server/main.cpp:27: warning: unused variable `std::string urls[2000]'

CompileC build/server.build/server.build/Objects-normal/ppc/html.o html.cpp normal ppc c++ com.apple.compilers.gcc.3_3
cd "/Volumes/OWC Mercury Elite/proxyproject/server"
/usr/bin/gcc-3.3 -x c++ -arch ppc -pipe -Wno-trigraphs -fasm-blocks -fpascal-strings -Os -mtune=G4 -Wno-four-char-constants -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -fmessage-length=0 "-F/Volumes/OWC Mercury Elite/proxyproject/server/build" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/include" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/DerivedSources" "-Wp,-header-mapfile,/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/server.hmap" -c "/Volumes/OWC Mercury Elite/proxyproject/server/html.cpp" -o "/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/html.o"
/Volumes/OWC Mercury Elite/proxyproject/server/html.cpp: In function `void replace_relative(std::string&, const std::string*, std::ostream&)':
/Volumes/OWC Mercury Elite/proxyproject/server/html.cpp:91: warning: `int len' might be used uninitialized in this function

CompileC build/server.build/server.build/Objects-normal/ppc/strings.o strings.cpp normal ppc c++ com.apple.compilers.gcc.3_3
cd "/Volumes/OWC Mercury Elite/proxyproject/server"
/usr/bin/gcc-3.3 -x c++ -arch ppc -pipe -Wno-trigraphs -fasm-blocks -fpascal-strings -Os -mtune=G4 -Wno-four-char-constants -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -fmessage-length=0 "-F/Volumes/OWC Mercury Elite/proxyproject/server/build" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/include" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/DerivedSources" "-Wp,-header-mapfile,/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/server.hmap" -c "/Volumes/OWC Mercury Elite/proxyproject/server/strings.cpp" -o "/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/strings.o"

CompileC build/server.build/server.build/Objects-normal/ppc/auth.o auth.cpp normal ppc c++ com.apple.compilers.gcc.3_3
cd "/Volumes/OWC Mercury Elite/proxyproject/server"
/usr/bin/gcc-3.3 -x c++ -arch ppc -pipe -Wno-trigraphs -fasm-blocks -fpascal-strings -Os -mtune=G4 -Wno-four-char-constants -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -fmessage-length=0 "-F/Volumes/OWC Mercury Elite/proxyproject/server/build" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/include" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/DerivedSources" "-Wp,-header-mapfile,/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/server.hmap" -c "/Volumes/OWC Mercury Elite/proxyproject/server/auth.cpp" -o "/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/auth.o"

CompileC build/server.build/server.build/Objects-normal/ppc/untitled.o untitled.cpp normal ppc c++ com.apple.compilers.gcc.3_3
cd "/Volumes/OWC Mercury Elite/proxyproject/server"
/usr/bin/gcc-3.3 -x c++ -arch ppc -pipe -Wno-trigraphs -fasm-blocks -fpascal-strings -Os -mtune=G4 -Wno-four-char-constants -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -fmessage-length=0 "-F/Volumes/OWC Mercury Elite/proxyproject/server/build" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/include" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/DerivedSources" "-Wp,-header-mapfile,/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/server.hmap" -c "/Volumes/OWC Mercury Elite/proxyproject/server/untitled.cpp" -o "/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/untitled.o"

CompileC build/server.build/server.build/Objects-normal/ppc/sockets.o sockets.cpp normal ppc c++ com.apple.compilers.gcc.3_3
cd "/Volumes/OWC Mercury Elite/proxyproject/server"
/usr/bin/gcc-3.3 -x c++ -arch ppc -pipe -Wno-trigraphs -fasm-blocks -fpascal-strings -Os -mtune=G4 -Wno-four-char-constants -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -fmessage-length=0 "-F/Volumes/OWC Mercury Elite/proxyproject/server/build" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/include" "-I/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/DerivedSources" "-Wp,-header-mapfile,/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/server.hmap" -c "/Volumes/OWC Mercury Elite/proxyproject/server/sockets.cpp" -o "/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/sockets.o"

Ld "/Volumes/OWC Mercury Elite/proxyproject/server/build/server"
cd "/Volumes/OWC Mercury Elite/proxyproject/server"
/usr/bin/g++-3.3 -o "/Volumes/OWC Mercury Elite/proxyproject/server/build/server" "-L/Volumes/OWC Mercury Elite/proxyproject/server/build" "-F/Volumes/OWC Mercury Elite/proxyproject/server/build" -filelist "/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/server.LinkFileList" -lstdc++ -arch ppc -prebind
ld: multiple definitions of symbol _Z7striposSsSsi.eh
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of absolute _Z7striposSsSsi.eh (value 0x0)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/html.o definition of absolute _Z7striposSsSsi.eh (value 0x0)
ld: multiple definitions of symbol remove_html(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of remove_html(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) in section (__TEXT,__text)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/html.o definition of remove_html(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) in section (__TEXT,__text)
ld: multiple definitions of symbol remove_element(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of remove_element(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >) in section (__TEXT,__text)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/html.o definition of remove_element(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >) in section (__TEXT,__text)
ld: multiple definitions of symbol fetch_page_reqs(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of fetch_page_reqs(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*) in section (__TEXT,__text)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/html.o definition of fetch_page_reqs(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >*) in section (__TEXT,__text)
ld: multiple definitions of symbol replace_relative(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const*, std::basic_ostream<char, std::char_traits<char> >&)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of replace_relative(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const*, std::basic_ostream<char, std::char_traits<char> >&) in section (__TEXT,__text)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/html.o definition of replace_relative(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const*, std::basic_ostream<char, std::char_traits<char> >&) in section (__TEXT,__text)
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)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of stripos(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int) in section (__TEXT,__text)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/html.o definition of stripos(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int) in section (__TEXT,__text)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/strings.o definition of absolute _Z7striposSsSsi.eh (value 0x0)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/strings.o definition of stripos(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int) in section (__TEXT,__text)
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> >&)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of 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> >&) in section (__TEXT,__text)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/auth.o definition of 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> >&) in section (__TEXT,__text)
ld: multiple definitions of symbol _Z11process_urlRSsSs.eh
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of absolute _Z11process_urlRSsSs.eh (value 0x0)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/sockets.o definition of absolute _Z11process_urlRSsSs.eh (value 0x0)
ld: multiple definitions of symbol process_url(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/main.o definition of process_url(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >) in section (__TEXT,__text)
/Volumes/OWC Mercury Elite/proxyproject/server/build/server.build/server.build/Objects-normal/ppc/sockets.o definition of process_url(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >) in section (__TEXT,__text)
ld: warning prebinding disabled because dependent library: /usr/lib/system/libmathCommon.A.dylib can't be searched
Quote this message in a reply
Moderator
Posts: 434
Joined: 2002.09
Post: #18
The .h file shouldn't include the .cpp file. The .h file usually contains only declarations. (In this case, declaration of your stripos function.) Any .cpp file that wants to use the functions (and the single .cpp that defines those function) should include the header file. Like so:

Header file:
Code:
/*
* strings.h
*/
#ifndef STRINGSH
#define STRINGSH

int stripos (const string s , const string find, const int offset);

#endif STRINGSH
CPP file:
Code:
/*
* strings.cpp
*
*/
#include <iostream>
#include <string>
#include <cctype>

#include "strings.h"

using namespace std;

// the following function definition should match the declaration in the header
// if it doesn't, and other code tries to use the function as declared,
// you may get "symbol not defined" errors from the linker
int stripos (const string s , const string find, const int offset)
{
   if ( offset >= (int)s.length())
   {
       return false;
   }
   // .. etc ..
}

Also, the name "strings.h" makes me nervous. It's hard to imagine a more common name for a header file! I don't know what else you are planning to put it in but I would call it stringutils.h.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #19
Yep, you've got it totally backwards. Source files include header files, not the other way around. And I certainly agree with Matt - name it something other than "strings".

Wade
Quote this message in a reply
Atomical
Unregistered
 
Post: #20
Quote:/*
* stringutils.h
* server
*
* Created by Adam on Tue Jul 05 2005.
* Copyright © 2005 __MyCompanyName__. All rights reserved.
*
*/
#ifndef STRINGSH
#define STRINGSH
using namespace std;
int stripos (const string s , const string find, const int offset);
#endif


Quote:/*
* stringutils.cpp
* server
*
* Created by Adam on Tue Jul 05 2005.
* Copyright © 2005 __MyCompanyName__. All rights reserved.
*
*/
#include <iostream>
#include <string>
#include <cctype>

#include "stringutils.h"

using namespace std;

int stripos (const string s , const string find, const int offset){
//tolower(find);
if ( offset >= (int)s.length()){
return false;
}
int i = offset;
int len;
int count;
int b;
int before;
len = find.length();
count = 0;
//i don't want to call this over and over
while ( s[i] ) {
before = i;
count = 0;
b = 0;

while (s[i] && find[b] && tolower(s[i]) == tolower(find[b]) ) {
count++; i++; b++;
}
if ( count == len) {
//where it starts
return before;
}
i = before;
i++;
}
return 0;
}

I tried removing the using namespace std; from the header file but it gave me an error.
Quote this message in a reply
Post Reply 

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