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.
Related
I've seen similar questions asked yet they still do not make sense to my ape brain.
Here is an example. If I declared a function in a header file named Bob.h: void PrintSomething(); and in the .cpp file I say: void MyClass::PrintSomething(){std::cout << "Hello";} . I've seen people in another .cpp file for example Frank.cpp, only include the Bob.h header which just has the declaration (No code inside it) and not the .cpp with the code but then what blows my mind is when they call the PrintSomething() function in Frank.cpp it uses the code from Bob.cpp and prints "Hello". How? How does it print "Hello" which was added in the .cpp file when I've only included the .h file which doesn't say anything about "Hello", its just a declaration? I've looked through the compile process and linking process too but it just doesn't stick.
On top of which if I were to now say in my Frank.cpp file: void MyClass::PrintSomething(){std::cout << "Bye";} and included the Bob.h file in my main.cpp and called the PrintSomething() function would it print "Hello" or "Bye"? Is the computer psychic or something? This concept is the one thing I am not grasping in my C++ learning journey.
Thanks in advance.
The moment you include Bob.h the compiler has everything it needs to know about PrintSomething(), it only need a declaration of the function. Frank.cpp does not need to know about Bob.cpp which defines PrintSomething().
All of your individual cpp files output object files generated by the compiler. These in themselves don't do much until they're all glued together, this is the linker's responsibility.
The linker takes all your object files and fills in the missing parts:
Linker talk:
Hey, I see that Frank.obj uses PrintSomething() and I can't see
its definition in that object file.
Let's check the other object files..
Upon inspecting Bob.obj I can see that this contains a usable
definition for PrintSomething(), let's use that.
This is of course simplified but that's what a linker does in short.
After this is done you get your usable executable.
on top of which if I were to now say in my Frank.cpp file: void MyClass::PrintSomething(){std::cout << "Bye";} and included the Bob.h
file in my main.cpp and called the PrintSomething() function would it
print "Hello" or "Bye"? Is the computer psychic or something?
The linker would find 2 definitions of PrintSomething() and would emit an error, it has no way to know what definition is the right one to pick.
The key notion here is separate compilation. You divide your project into a set of source files that implement more-or-less independent things, you compile those source files into object files, and you link the object files and any additional libraries (including the standard library) to create an executable file. For large projects, compiling all of the source files can take a long time (sometimes measured in hours). The first time you build your application you have to do that. But after that, if you only changed one source file you only need to recompile that source file and then link again, which the object files that you created the first time through. That's usually a big time saver. If you have one massive source file (i.e., a source file that #includes all the rest of your source files), you don't get that option -- you have to recompile the whole thing every time.
I'm getting the error
Symbol(s) not found for architecture x86_64
Trying to compile a project on QtCreator. It happens when I try to create an instance of an user defined class, Layer. That class consists of a header, layer.h, and a implementation, layer.cpp. It was tested and works in another programs. On my project, it is included in qtwidget.h and the error happens when I try to use it on qtwidget.cpp. For example:
Layer<double> text("pq.txt",0.5,0.5,0.5);
Having this line on qtwidget.cpp is enough for the error to show up.
This is such a generic error that I'm clueless on how to isolate it any further, but if it helps, I've included the whole project on this git repo.
In my opinion, the error message that Qt Creator displays is quite misleading until you understand it, but does not prevent splitting the template class into a header and implementation file. If you think about the message:
Symbol(s) not found for architecture x86_64
the problem, I originally thought when I saw this, is that it states this error on its own in the Issues output and can lead the user into thinking that the problem is due to the architecture. Actually, all its saying is that there's a defined symbol (often function) whose matching implementation wasn't found.
If you change from Issues to the Compile Output window and scroll up, you'll be able to see exactly what symbols can't be found; mine's displayed in red. It's just annoying that the detail of the missing symbol(s) doesn't show up in the Issues view.
It's easy to replicate this error by just adding a function definition into a header and without implementing the function, call it from the .cpp file. You'll then see something like this in the Issues window
Switching to the Compile Output view and scrolling up displays this: -
So now we see that tthe actual problem is that the function DoSomeStuff in the class called PGGui is being called from the constructor PGGui::PGGui, but the body of DoSomeStuff is missing, as its symbol is not found.
Fortunately I've managed to solve my problem before any answers, so, if anyone is experiencing anything similar, the issue was that it seems you can't split a templated class into a .cpp and a .h file. Putting all declarations of the .cpp file back into the .h solved the issue.
I still had a leftover problem, though: duplicated symbols (which was the reason I split it). This time, declaring a variable as external in the .h, and redeclaring it without the external keyword in one (and only one) .cpp file solved the issue for good.
For me this problem resulted from not rebuilding the make file after adding another source file and header.
Under Build:
Cleanall->run qMake->run
fixed the error for me.
The problem in my case was that i had a project with many subproject and one of the pro files of the subprojects was empty.
For me, I forgot to write the name of the class object when declaring the function in .cpp file.
wrong: int Zero(int &num)
right: int Common2::Zero(int &num)
Where Common2 is the class.
I am very new to c++ and am doing a tutorial. I have copied the tutorial exactly but on compiling get this error:
'String file not found'
for the line #include <string>;
Could someone tell me how to amend this?
Ok, so I changed the name of my file from .C to .cpp and this particular issue seems to have gone.
You seem to have found a solution, I'm adding this to clarify why this is happening. Some compilers integrated with IDEs treat .c files as C source code an .cpp (or .cc, .c++, etc.) as C++ code. As you compile a .c file, C++ support isn't included and a C compiler is used instead of a C++ one. And C doesn't have <string> (although it does have <string.h>, but that contains entirely different stuff).
It looks like your compiler isn't correctly or fully installed. The compiler should be able to find its own headers without further effort on your part.
Ok, so I changed the name of my file from .C to .cpp and this particular issue seems to have gone. However, I now get 3 Apple Mach-o Linker (Id) errors (?)
As this is different to the original questions I will close this and open a new one
Thanks for all the help!
check the location c:/...../include
If exist string file should reinstall compiler
I've been looking for solutions for this problem but them didn't work for me! I've been having a lot of problems compiling programs.
When I'm trying to execute a program in Xcode appears:
duplicate symbol _main in /Users/LauraChaparro/Library/Developer/Xcode/DerivedData/Busqueda-ercduihvfosqcoczkrgljkkmgxam/Build/Intermediates/Busqueda.build/Debug/Busqueda.build/Objects-normal/x86_64/Uno.o and /Users/LauraChaparro/Library/Developer/Xcode/DerivedData/Busqueda-ercduihvfosqcoczkrgljkkmgxam/Build/Intermediates/Busqueda.build/Debug/Busqueda.build/Objects-normal/x86_64/main.o for architecture x86_64
I don't understand why this happens! Is a C++ project... Or if you can recommend me another IDE I'll be really thankful!
It looks like you are defining a main in some of the code used to make the Uno.o object file. You can only have one main, so objects used to build libraries should not define a main. This should only be defined in the application itself.
This a linker error and it likely you would encounter it with other IDEs. You need to find where in Uno.o the main is defined and remove it.
The error you have there is a linker error. It tells you exactly what is wrong. The linker has found out that it can find the symbol *_main* defined more than one time looking through the object files. This makes it impossible for the linker to create an executable of your object files, as it have no way of telling which symbol you want to use. It continues to tell you where it found the two symbols.
Once in the Uno.o file (which I will assume come from compiling the Uno.cpp file)
another from the main.o file (from the main.cpp file)
This means, that somehow the linker can find the symbol for the main method in both files.
This could happen by:
Defining a main function both in the Uno.cpp and main.cpp
Having placed a non inline main definition in a header and including this both in the Uno.cpp file and the main.cpp file (or an include that includes another include and so on.)
As your request for another IDE I would like to remind you that neither compiler errors or linker errors are errors in the IDE, in an overwhelming number of cases the problem is that the user of the IDE has done something wrong.
This happened to me once, Make sure you include your .h file instead of your .c file in the file you don't expect your main function to be. I had my include statement having .c file and it produced the same issue .
I will be very grateful, if somebody can actually explain what exactly my compiler does when I press button BUILD, and compiler begins to compile all my .h and .cpp files how exactly this process is going on(what do I have inside object file?), why do I ask such question? I'm trying to understand what does it mean "minimize compilation dependencies between files" from book of Meyers about 50 specific ways...(hope You know about this book),there he explains what does it mean Abstract Base Class and Handle Classes, as my lecturer explained me I don't need to include excessive .h files and thats all, any links about compilation process will be appreciated as well, thanks in advance for any help
When doing a full compile, the compiler will read each .cpp file in turn. For a given .cpp file it will then read every file referenced by a #include directive, recursively, compiling the code as it goes. When it compiles the next source file it will read the files referenced with #include in that source file.
When you make any changes and do a build, then if any of the files referenced by a #include directive from your .cpp file have changed then the .cpp file will be recompiled, as if the .cpp file itself has changed.
Unnecessary #include directives thus have two costs: firstly the compiler has to read and process more files when compiling, and secondly it increases the chances that your .cpp file will need recompiling even if nothing it actually uses has changed.
See
http://computer.howstuffworks.com/c2.htm
for a introduction and
http://www.tenouk.com/ModuleW.html
for an in depth descirption
Additionally, some theoretic background can be found at
http://en.wikipedia.org/wiki/Compiler
The best way to understand how a compiler works is to first understand how an assembler works. There is a decent explanation here.