IOS duplicate symbol _ main in: Solution
Today, I have dug a hole for myself. Some files in the project are C/C ++ Files written by the background friends, so I imported them all at once, however, during compilation, duplicate symbol _ main in:. o and B. o compilation error.
Below is a simple Demo to restore the scenario.
Create a project and create the Hello. h and Hello. c files. The Code is as follows:
Hello. h
#ifndef __Main__Hello__#define __Main__Hello__#include
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 that can be recreated.}@end
The specific error message during compilation is 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 error cause is obvious (because my Demo is too simple ):
There is a main function in the Hello. c file, and there is a symbol _ main when compiled into the Hello. o file.
There is also a main function in the original main. m file of the project, and there is also a symbol _ main during compilation into the main. o file.
However, there can only be one main in a project, so there is a conflict.
Solution: do not call the main function in the C/C ++ file, or rewrite the function name. You cannot change the function return type or parameter list.
In the future, we will encounter problems such as duplicate symbol, which can be considered from this perspective. The problem is that we often encounter methods or functions with the same name in our project, therefore, conflicts occur during compilation. The error message during compilation. o and B. o identifies the two duplicate symbrs that appear in the two files, and then opens the two files in the project to search for conflicting method names for comparison and modification.