Xcode 4.2 - c++ include guard structure -
i seem doing wrong way use include guards. of time structuring works, in circumstances code below, have problems. might causing problem me using header file "all.h" big collection of other header files (like "another.h" , other header files needed).
the code compile if code in file "another.cpp" commented out, somewhere along line there duplication of function "sleepfunc" (i think), since following error:
apple mach-o linker (id) error
ld: duplicate symbol sleepfunc(unsigned int) in
/users/(project path)/.../another.o ,
/users/(project path)/.../main.o architecture x86_64
command /developer/usr/bin/clang++ failed exit code 1
i using xcode version 4.2 on mac os x snow leopard (10.6.8).
during typing of post, discovered issue, me including header "all.h" in "another.cpp". if thing had (#include in "another.h", use header "another.h" in file another.cpp), makes me unhappy, since means files need other files start getting messy. have 1 header file each new file make.
(and question, why did compiler duplicate "sleepfunc", include guards???)
is there better, more clean way structure include guards and/or includes?
all.h
#ifndef includertest_all_h #define includertest_all_h #include <iostream> #include <stdlib.h> #include "another.h" void sleepfunc(unsigned milliseconds); #ifdef _win32 #include <windows.h> void sleepfunc(unsigned milliseconds) { sleep(milliseconds); } #else #include <unistd.h> void sleepfunc(unsigned milliseconds) { usleep(milliseconds * 1000); // takes microseconds } #endif #endif
main.cpp
#include "all.h" int main (int argc, const char * argv[]) { sleepfunc(500); printf("hello world!"); return 0; }
another.h
#ifndef includertest_another_h #define includertest_another_h class another{ public: void spunky(); }; #endif
another.cpp
#include "all.h" void another::spunky(){ printf("very spunky"); }
because include all.h in 2 cpp files have symbol repeated. try put implementation of sleep third cpp file or use static void functions. include guards okay see.
Comments
Post a Comment