简体   繁体   中英

Using and referencing multiple cpp files in Xcode 4

I'm new to programming, and I'm currently working on a project that got a little too big to keep in a single cpp file, so I decided to split it up into its constituent parts in different cpp files.

The project has 3 cpp files;

main.cpp, TwinCameraCapture.cpp, FaceCalibration.cpp,

All of the meat of the code is in FaceCalibration.cpp and TwinCameraCapture.cpp, with main.cpp looking like this:

#include "FaceCalibration.cpp"
#include "TwinCamCapture.cpp"

int main ()
{
    FaceCalibration();

    TwinCameraCapture();
}

With FaceCalibration and TwinCameraCapture being the primary functions within their respective .cpp files.

Now, I know I'm doing something wrong here, I just don't know what exactly it is. The error I get when trying to compile is:

Apple Mach-O Linker (Id) Error

Command /Developer/usr/bin/llvm-g++-4.2 failed with exit code 1

Could someone please explain what I've done wrong and how to fix it, or point me to a guide that will do so?

Thanks

EDIT: The full error message is:

    ld: duplicate symbol getWindow(cv::Rect_<int>)   in
 /Users/facebooth/Library/Developer/Xcode/DerivedData/FullProgramTest-
awrdeznffntuoacjytwewfbjdmza/Build/Intermediates/FullProgramTest.build/Debug/FullProgramTest.build/Objects-normal/x86_64/FaceCalibration.o and 
/Users/facebooth/Library/Developer/Xcode/DerivedData/FullProgramTest-
awrdeznffntuoacjytwewfbjdmza/Build/Intermediates/FullProgramTest.build/Debug/FullProgramTest.build/Objects-normal/x86_64/main.o for architecture x86_64
    Command /Developer/usr/bin/llvm-g++-4.2 failed with exit code 1

Generally, you don't use .cpp files as #include. You should have a .h file that is included.

It would be helpful to know what your actual error message is, rather than just that "it failed", but I suspect it's "duplicate identfier " or some such - because your IDE is already compiling and linking your file TwincameraCapture.cpp, and then you go and make that piece of code part of your main source, whicn means the compiler generates the same code twice, and the linker says "Which one of these do you mean?" when it sees two functions with exactly the same name.

Simply rename the .cpp file that doesn't include the main() function to a .h file and then adjust the #include appropriately and it will work. I haven't got far enough along to get to headers to know why it is like that in xCode or anything but I do know that this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM