Xcode, Duplicate symbol _main - xcode

I have duplicate symbol _main.
The problem is it says "Duplicate symbol _main in /Users/.../i386/main-B9843B6026D6EFA4.o and /Users/.../i386/main-B9843B6026D6EFA4.o", the XXX and XXX are actually the same .o file. I don't know why it thinks it's duplicate symbol when it's the same .o?!
Any help appreciated, thanks.

Ah..I figure out it's that I have multiple entries under Targets/Compiled Sources ( in newer XCode it's under Build Phases/Compile Sources ). I removed them and the problem is solved. The multiple entry thing probably has to do with Git merge.

It appeared that in my case I was importing a .m file instead of its .h counterpart.
Solved by editing
#import "Tools.m"
into
#import "Tools.h"

I also had this problem and it was caused by code I imported from another project.
I did a grep for "int main" in my project directory:
grep -nr "int main" .
and found
./main.m:13:int main(int argc, char *argv[])
./IMPORTED_DIR/main.m:13:int main(int argc, char *argv[])
the IMPORTED_DIR contained the extra main.m that was causing the error for me
I removed that file from the Project -> Targets -> Build phases -> Compile sources list and then it compiled

I was facing the same issue with using two third party framework. (AppLovin and Flurry)
And I came to know that by removing "all_load" from "Other Linker Flags" in build settings.

I had the same problem opening a project, that was created with Xcode 4.0.2, with Xcode 4.1. I simply solved by clicking on "Modernize Project" (Editor/Modernize Project). This procedure automatically removed all duplicates.

If still have a problem, try to search like this: "int main(", and remove those files except main.m

Just got this problem myself, but after reading huggie's solution which did lead me on the right track, I was left a bit confused.
So current solution in Xcode:
Choose project, target -> build phases and click validate settings
Then Xcode will auto fix its own mistake. It is always nice when the tools tries to stop your progress ;)

In my case, I declared an NSString in my constants file (imported by many classes) but forgot to define it as static!!
e.g. NSString* PARAMS = #"paramA";
should be: static NSString* PARAMS = #"paramA";
Reading the full error message allowed me to figure this out: "Duplicate symbol PARAMS". Don't be afraid and try to understand error messages! Sometimes they might even tell you exactly what you did wrong.

In my case, I had imported another project, in order to utilize a library contained within. It resulted my project having two main.m files.
This was even more confusing, since the error didn't show up until several builds later.

You can get this for method names too!
I got duplicate symbol _runOnMainQueueWithoutDeadlocking after adding DBCamera via CocoaPods and it was because both my category on NSObject (NSObject+Tools.h) and the GPUImage dependency file GPUImageOutput.m both had a method called 'runOnMainQueueWithoutDeadlocking'.
I was fortunate enough to be able to remove my method from code because I wasn't actually using it anymore or anywhere.
It's probably what I deserve for putting a category on NSObject.

Related

`po` gives `error: <EXPR>:1:1: error: use of unresolved identifier`

I have this problem with po in the console where trying to output a function works in itself, but outputting a variable or constant doesn't.
As you can see here, although you'd think the var/let holds the content of bar.boy(), Swift somehow can't find it...
So it turns out there was probably a bug in the past that when you were adding a Swift file it would add/ask a Bridging header, but it wouldn't add that line to your project
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
which means you'd stay in this state
resulting in error: <EXPR>:1:1: error: use of unresolved identifier!
I could only find that out because I moved around files in my project and when I added ObjC files to the project, it asked me about a Bridging header (although I had one already!) and luckily added that SWIFT_OPTIMIZATION_LEVEL. One could consider this is a bug to consider the default value is fastest, but then again I guess this was only a bug in the past and got fixed now.
Still, I fixed now it might be a bug the other way around, if it add none in the release build. I can't test this right now because for testing this I only had a Debug build. I'll leave that as an exercise :) for Apple's Engineers.
I set Swift Complier to "-O"
SWIFT_OPTIMIZATION_LEVEL = "-O";
and set it back to "-Onone".
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Then it works.
Fixed after setting:
Optimization Level = None[-Onone] under Swift Compiler - Code Generation
Note that setting Optimization Level under Apple LLVM 8.0 - Code Generation has no effect
I bet that bug is due to the fact that foo has been optimized out during compilation and it's symbol does not exist anymore in the compiled code (even if it shouldn't have in debug and it's still an LLDB bug)
Probably if you add some usage of foo in the next lines (even a println) its symbol will hopefully be kept in the IR and you'll be able to po foo
(I agree that that's still a bug but at least if it works you'll have a workaround and some sense of explanation)
I encountered a similar issue but I already had the correct configuration, all optimisations set to None. However I still didn't get any values.
After some further digging I found out that the issue originated from Xcode being unable to the resolve the types:
(lldb) frame variable self
<could not resolve type>
That led me to this question: xcode 8 Debugger 'Could not resolve type' where the issue is described and the bridging header seems to cause issues.
With my project not having any bridging headers I studied the build settings once more and found this setting
This was initally set to YES. After changing the it to NO symbols are working again.
SWIFT_INSTALL_OBJC_HEADER = NO
If your project is using Swift, there are two separate "Optimization Level" settings in the project/target configuration. Its not only "SWIFT_OPTIMIZATION_LEVEL". Check this link: https://stackoverflow.com/a/32984193/2060180
I'll leave it here in case it's useful to someone.
For those who are using Swift framework in an Objective-c project, and wanna debug the Swift source files in that project: (In my case, I have a mixed-language module which managed by Cocoapods, I need to debug the swift module in my Objective-c Example project)
It works for me after I added an Empty Swift file in my Objective-c project.
Otherwise, those swift compiler options won't show up in the Build Settings.

unable to print object ('po') in xcode6 beta 6 osx swift project: (Error in auto-import: failed to get module '__ObjC' from AST context)

An attempt to print object (po command) in xcode 6 beta 6 OSX Swift project results in this error message:
(lldb) po managedObject
error: Error in auto-import:
failed to get module '__ObjC' from AST context
In this case the object in question is an instance of NSManagedObject.
Any advice on how to help auto-import in getting __ObjC module into LLVM Abstract Syntax Tree context?
As of Xcode 6.1 if you attempt the po command twice it will work on the second attempt. The first po command will always fail for each new debugging session but subsequent calls work.
Have same issue in xcode 7.3.1:
error: Error in auto-import:
failed to get module 'Touch' from AST context:
<module-includes>:1:9: note: in file included from <module-includes>:1:
#import "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/libxml2/libxml/tree.h"
^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/libxml2/libxml/tree.h:17:10: error: 'libxml/xmlversion.h' file not found
#include <libxml/xmlversion.h>
^
could not build Objective-C module 'LibXML2'
But you can use fr v managedObject instead of po managedObject.
I'd recommend double-checking that you have the
-D DEBUG
flag set under 'Other Swift Flags' for the scheme that you're using to debug. I experienced similar issues when I'd accidentally deleted it.
A clean of my project and deleting DerivedData worked for me.
I discussed a similar error message with an Apple engineer at WWDC2017. It seems like this issue can have many causes, and I am aware that mine is slightly different than the one described above.
My team spent weeks trying to figure this out, and it ended up being a bug on Apple's compiler, which we could never have figured out by ourselves. Also, it has a VERY easy workaround.
So, this is just me posting the fix here, in order to maximize the probability that someone else does a search for this confusing error message, and finds this answer.
So, here it is. In our case, we had an Objective-C project using a mix of Swift and Objective-C frameworks. This fix might apply in slightly different contexts, just try it.
There happens to be a bug with the way the compiling flags get aggregated from the frameworks and the project, and the "pure Objective-C" project "activates" it.
Solution: add one single, empty Swift file ("Whatever.swift", or whatever) in your Objective-C project, making it not-pure-objective-c any more (new->file->Swift file, don't create the bridging header. The file will only contain the import of Foundation).

Xcode produces warnings 'Implicit declaration of function N is invalid in C99' for C functions imported in .pch file

Unlike the behavior described in this SO topic: xCode 4.4 does not get all the .pch file headers imports?, my project does builds successfully, but anyway I see these annoying yellow highlights Xcode gives me in its editors.
How can I teach Xcode to pick up .pch-file headers imports or is this a buggy behavior?
This is very strange, but after restarting Xcode and cleaning build folder (just one more command+alt+shift+K in response to #Matthias comment) warnings have gone.
I say 'strange' because I did see them for about a week (after I had extracted a couple of small utility C functions as a Cocoa Pod and had begun importing them in my .pch-file).
I will provide more details here, if I ever see this behavior again and notice any details about how it can occur.
I fixed it by adding #include <arpa/inet.h> in .pch file of my xcode project

Xcode shows many errors but program compiles and runs fine (in both simulator and device)

After installing the CocoaLumberjack' log compressor class I've been getting this annoying behavior: Xcode complains that there are many undeclared identifiers and gives me many errors (not warnings but errors with the red icon).
The thing is that I can compile and run my iPad app just fine but Xcode won't do any autocompletion. I tried cleaning the build folder (Product > option + Clean), and also deleting derived data. I've also rebooted to no avail.
As you can imagine this is a pain to work with. I did have this behavior happen before on a previous version of Xcode; it had something to do with stuff in my precompiled headers file but using the solution above would always fix it. I'm currently using Xcode 4.4 (4F250).
Sample error I'm getting:
Semantic Error: use of undeclared identifier 'DDTTYLogger'
The above happens even with classes that I wrote myself and that have not changed since installing the CocoaLumberjack compressor class.
I finally solved this after MANY attempts using the following:
Remove the last #import from my Prefix.pch and build again. Errors would happen (obviously). Put the line back and build again. No errors would show and after 10 seconds or so, errors would come back again.
Repeat the above except instead of the last #import, remove the last TWO imports, then three, four, etc. I did this until I removed five imports and when I put them back and waited, Xcode stopped complaining.
Note that this didn't occur to me at all. I read this solution on a blog somewhere.
Weird bug...
Open build settings and set "Precompile Prefix Header" to "No", that solved my problem.
Kudos for: https://stackoverflow.com/a/7035492/936957
I've been running into these issues constantly on all the latest versions of Xcode, in both Objective-C and Swift.
I noticed today that I was getting the errors in one particular class file. I removed it from some extra targets it was in and the errors finally went away!
I think Xcode has some fundamental bugs with it's handling of multiple targets right now. My theory is that if the other target is not built, you will essentially see errors from that target. Anyway hope this helps someone.
Not bad,
If you follow these Steps-
1-Clean Xcode(Cmd+Shif+K).
2- Clear Derived Data(Cmd+Shift+G).
Enter this path( ~/Library/Developer/Xcode/DerivedData/).
3- Quit and open again Xcode.
This problem can cause by setting "Target Membership" for some files are not the same.
Example:
A class XYZ put in file "a.swift" and it's used in file "b.swift". But "Target Membership" setting of "a.swift" is not the same "Target Membership" setting of b.swift.
Check "Target Membership" setting as below:
I was having issues with a library installed via cocoapods. Going to Build Settings and searching for 'Allow Non-modular Includes In Framework Modules' then setting it to Yes did the trick.
I had it on Xcode 10.1 when I accidentally pressed:
Cmd+Shift+U - ( build for Testing )
try Clean (Cmd+Shift+K) and then:
Cmd+Shift+R ( build for Running )
After update to Xcode 11, I met the same problem. I tried all the mentioned advices (cleaning folders, turning on/off different settings, restarting xCode), but nothing helped. Also, I have a big project in C, so, I'd like to keep using precompiled headers.
Finally I found that simple restart of Mac OS solves the problem! It's really weird behaviour, but I'm happy anyway that I found a solution – it's hard to code when lots of colourful error messages float around.
For me it helped cleanning the project. XCode->Product->Clean
I got the similar type of issue.
Alternate option to fix this is
Open organizer and delete the derived data of your project or delete all the projects in organizer projects tab. It works fine..
I just had the same thing in Xcode 5.1.
I fixed it by making sure there were no blank lines between #import's
I have removed some extra spaces and extra lines from .pch file and it xcode stopped complaining
This happened to me as well, but cleaning didn't fix it. What did was quitting and reopening XCode. Afterwards, all the phantom errors were gone. For those wondering, the tabs you have open when you close will still be open when you reopen.
I had this issue recently. It can be remedied in some cases by deleting the ModuleCache folder inside DerivedData, along with the project folder in DerivedData. Note that Xcode must be quit before doing this.
Running on Xcode Version 10.1 (10B61), I set the build setting "Increase Sharing of Precompiled Headers" to NO. I was working in an .xcworkspace with many projects sharing the same frameworks, and no Objective-C bridging header (meaning I've added no obj-c code myself). I'm not sure when Xcode did away with .pch files by default, but I didn't have any of those in my project.
Open up a terminal and create a nice little function accessible via the command line...
nano ~/.bashrc
add (making the necessary substitutions between the pointy braces)
cycle() {
git stash save "BACKUP"
git checkout <<SOME OTHER BRANCH>>
git branch -D $1
xcodebuild -allowProvisioningUpdates -workspace <<YOUR WORKSPACE>>.xcworkspace -scheme <<YOUR SCHEME>> -configuration Release clean
git checkout $1
}
^X and save it by following the prompts, then enter source ~/.bashrc to make it visible to the current terminal session.
Make sure your branch is pushed to origin, cause we're going to delete it :)
Call the function using cycle <<MY BRANCH>> (once it's run you might want to call git stash pop to restore any working copy changes)
Hope it works for you! Xcode, get on your game!
In my case (mixed objc/swift project) at least part of errors were caused by absence of imports for some used frameworks, e.g. "import UIKit". Project was compiled successfully because frameworks were anyway included in headers in Prefix.pch file. But errors were shown, for example about not finding method defined in UILabel extension, and yes, this extension was without "import UIKit". So I think these errors in most cases depends on Prefix.pch precompilation/updating.
Touching Prefix.pch, cleaning, removing derived data, closing/opening XCode sometimes helps, but not always.
Using SPM, it can be that the Module you import, which you use at the location of the errors, has missing dependencies itself.
In my case I had an error about a missing initializer, XCode trying to use a different one than the one already defined in the other Module. But the project compiled fine! Nothing worked at first. Then I tried first cleaning, and then Product -> Perform Action -> Compile "myfile.swift" and it showed what the missing dependency in the dependency was! Note: This goes recursive. In multiple places a depenendency may have not been declared in the package.swift -> compile the same file repeatedly until it compiles successfully.
So.. it wasn't that the module with the error had a missing dependency, but rather that the direct dependency did miss a Module-dependency declaration in its package.swift declaration.
My explanation is that to generate the Error-output of XCode, Modules get compiled alone on their own. Import errors then show up. But when Modules are built as part of a larger Module, then that large module may import the missing dependency of the broken sub-Module already, making the build pass.

Xcode debugger sometimes doesn't display variable values?

This happens to me pretty often. For example, right now I have the debugger stopped at a breakpoint in a method . . . and it isn't displaying any variable values at all. Other times, it displays some, but not others.
Can anyone explain?
The most common reason for this is that you're trying to debug code compiled with optimisation enabled and/or no debug symbols. Typically this will be because you're trying to debug a Release build rather than a Debug build but it can also happen with Debug builds if you've made inappropriate changes to the Debug build settings.
Another less common possibility is that you've hosed the stack.
I had this issue (using Swift), I spent ages crawling through my git commits to find where to problem started.
For me, I was using Facebook Tweaks library, but I was (unnecessarily) importing it from my project-bridging-header.h file.
Once I got rid of it, I got my debugging back.
for example, in my bridging header I had:
#ifndef PROJECT_Bridging_Header_h
#define PROJECT_Bridging_Header_h
// Facebook Tweaks
#import "FBTweak.h"
#import "FBTweakStore.h"
#import "FBTweakCategory.h"
#import "FBTweakCollection.h"
#import "FBTweakViewController.h"
#import "FBTweakShakeWindow.h"
#endif
I removed all the imports and just imported it as usual in my AppDelegate import Tweaks.
e.g:
#ifndef PROJECT_Bridging_Header_h
#define PROJECT_Bridging_Header_h
// Removed Facebook Tweaks
#endif
and in my AppDelegate.swift
import Tweaks
This fixed all my debugging issues, everything works as expected and I can also using Facebook Tweaks.
Note: I don't think this is an issue with Facebook Tweaks itself, you may have some other library causing the same issue. The idea is to remove things from your bridging-header one by one and see if you can narrow down the issue.
I think I read somewhere that if a library is causing many issues behind the scenes, this can stop your debugger working.
If this doesn't help, try crawling through your git commits and see at what stage the debugging stopped.
other similar issues on SO:
Xcode Debugging not showing values
Xcode debugger doesn't display variable information after installing CocoaPods Podfile
If you're having similar issues hope this helps! 👍
A possible solution is to set the Optimization Level for your current target Debug scheme to none.
Project -> Target -> Build settings -> Optimization level -> Debug (or whatever fits your project) -> None
Source:
https://stackoverflow.com/a/14948486/3590753
I've had similar issues using LLDB. Switching it back to GDB seems to address it. Obviously this isn't solving the problem, but its a workaround anyway
My issue was that I had address sanitizer enabled. Disabling sanitizer resolved my issue in XCode 8.2.1
You can get the value of any variable in the console by writing:
po name_of_an_objectCVar
or
print name_of_a_cVar
If your breakpoint has "automatically continue after evaluating options" set, then it won't write to the variable view - FYI
I know this is old, but i ran into same problem too. I could not see any summaries of any objects, just types and some address code. After 4 hours of struggling with compilers, debuggers and other solutions i was about to give up when by accident i found this option in debugger. "Show Summaries". Just by clicking it everything got fixed and now i see all variable summaries!
Had the same issue using Xcode 6.4 running the app on device. Running on simulator will show all variables on debugging variables panel.
There is a situation I have seen where Xcode can't cope with return value optimisation (RVO) -- if the compiler decides to apply RVO to a variable then it may not appear in the variables list. You can disable this in g++ and clang with the compiler flag -fno-elide-constructors
See also Understanding eliding rules with regard to c++11
For Swift mix OC Project which use pod
Fixing it by removing useless header(that import with framework by pod) xx-Bridging-Header.h
eg.
In the past I import header with #import "GCDAsyncSocket.h" which I was added in podfile
platform:ios, '8.0'
use_frameworks!
target "roocontrollerphone" do
pod 'CocoaAsyncSocket'
end
just remove it in that xx-Bridging-Header.h file
If you are using the #property feature of Objective-C 2.0 the debugger does not display those variables unless they are backed by explicit ivars in your Class interface. This is slated to be fixed in Xcode 4 as I understand it.
temporary solution when it happpen to me :
right click on the property
jump to definition (u can do it manually and scroll to the #synthesize in the top of the file)
now, if the line is like this :
#synthesize myObject = _myObject ;
set the mouse cursor on the "_myObjects". that what worked for me..when i have problems.
I figured out why it is not working in XCode 4.6 - all of the variables in my object, self, were declared in the .m file instead of the .h. When I moved one of them back to the .h file, it showed up in the debugger. Sounds like a bug with XCode in that it cannot "see" variables declared in the implementation file.
For me it works changing the content of display variables panel to Local Variables and then back to Auto.
This solution worked on XCode 6.3.2, Swift type project.
You need to disable two types of build optimizations in the build settings. By default, the "swift compiler - code generation" optimization level for debug build is set to fast. You need to set this to none. Also check that the "apple llvm 7.1 - code generation" optimization is set to none for debug build.
Finally, check that you are building the debug build in the "architectures" section of your build settings.
Hope this helps.
I have been stuck a while with this problem and finally find out a solution.
I think that many reason can causes this bug but in my case here is the solution.
While you are in the breakpoint position check the included classes.
I was including using double quote a file which was located using include path.
#include "MyClass.h"
instead of
#include <MyPorject/MyClasses/MyClass.h>
So if you have this problem try to double check your inclusion and import.
I know it seems weird but worked for me and I have been able to reproduce it by putting back the Double-Quote include.
One possible reason for the debugger displaying seemingly wrong values is that the variable type is of Any?.
E.g.
var a: Any? = 12
var b: Int? = a as? Int // b=13483920750
var c: Int = a as? Int ?? 0 // c=1
In the example above, b holds the correct value of 1 even though it is not displayed as such.
I've had the same issue and I fixed it by reinstalling all Pods. Just delete them and install again.

Resources