Today hand to dig a hole for oneself, because some files in the project is in the background of the small partners write C/s + + files, so I import all at once, but the compilation appeared duplicate symbol _main IN:A.O and b.o compile errors.
Here's a simple demo to restore this scenario.
Create a new project, and then create a new Hello.h and hello.c file with the following code:
Hello.h
#ifndef __main__hello__#define __main__hello__#include <stdio.h>int Main (), #endif/* defined (__main__hello__) * /
hello.c
#include "Hello.h" int main () { return 0;}
Then use the main function in VIEWCONTROLLER.M:
#import "ViewController.h" #import "Hello.h" @interface Viewcontroller () @end @implementation Viewcontroller -( void) Viewdidload { [super viewdidload]; Main ();} -(void) didreceivememorywarning { [super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
compile-time specific error messages are as follows:
Duplicate symbol _main in: /users/apple/library/developer/xcode/deriveddata/main-alxynewqhvyqdafbjcjfzntmqfml /BUILD/INTERMEDIATES/MAIN.BUILD/DEBUG-IPHONESIMULATOR/MAIN.BUILD/OBJECTS-NORMAL/I386/HELLO.O /Users/apple/ library/developer/xcode/deriveddata/main-alxynewqhvyqdafbjcjfzntmqfml/build/intermediates/main.build/ Debug-iphonesimulator/main.build/objects-normal/i386/main.old:1 duplicate symbol for architecture I386clang:error: Linker command failed with exit code 1 (use-v to see invocation)
the reason for the error is obvious (because my demo is too simple):
The hello.c file has a main function that has a symbol _main when it is translated into a hello.o file.
In the original main.m file of the project there is also a main function, in the translation into the MAIN.O file also has a symbol _main.
There is only one main in a project, so there is a conflict.
Workaround: Do not invoke the main function in a C + + file, or rewrite the function name, but it is not possible to change the function's return type or parameter list.
Later encountered duplicate symbol and other problems, can be from this point of view, the problem is often in the project we have some of the same name of the method or function, resulting in the compilation of conflicts. The compile-time error message, A.O and B.O, identifies the two files in which the two duplicate symbols appear, and then opens the two file search conflict method name in the project to compare the changes.