IOS duplicate symbol _ main in: Solution

Source: Internet
Author: User

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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.