Clang Error "duplicate symbols for architecture x86_64" - ios

This error is listed a lot over stack and the common fixes don't seem to work for me. I am getting the error: "duplicate symbols for architecture x86_64" when compiling an Obj-C MacOS Xcode App. Xcode 11.5 and MacOS Catalina 10.15.5. It is saying there are 15 duplicate symbols, with one of the 15 errors looking like:
duplicate symbol '_visual10Window' in:
/Users/tempuser/Library/Developer/Xcode/DerivedData/test-cdicjztmkktfomehwaphnzhbozgh/Build/Intermediates.noindex/test.build/Debug/test.build/Objects-normal/x86_64/Test.o
/Users/tempuser/Library/Developer/Xcode/DerivedData/test-cdicjztmkktfomehwaphnzhbozgh/Build/Intermediates.noindex/test.build/Debug/test.build/Objects-normal/x86_64/MainView.o
My MainView.h and MainView.mm do not contain any definition for visual10Window.
My Test.h has (removing this give a use of undeclared identifier error).
#class Visual10;
Visual10 *visual10Window;
and Test.mm includes
#import "Visual10.h"
and I'm calling a xib from a menu item in Test.mm
- (void)Visual10Menu:(id)sender {
if (!visual10Window) {
visual10Window = [[Visual10 alloc] initWithWindowNibName:#"Visual10"]; }
[visual10Window showWindow:self]; }
I have tried the common suggestions including:
Install and reinstall pods (this project doesn't use pods)
Checked for wrong imports like import ".m" instead of ".h"
Remove -all_load from Other Linker Flags
Build Settings / No Common Blocks set to NO
Checked for duplicate files Build Phases / Compile Sources (None)
Tried adding and removing -ObjC from Other linker flag
Tried Build Settings / Enable Testability set to NO
Tried clearing derived data
None of these work and still get duplicate symbol errors.

Related

Linker Error while testing: Undefined symbols for architecture x86_64

I am working to support iOS 11 and XCode 9. When I try to execute the test suite, I receive a Linker Error:
Undefined symbols for architecture x86_64
"ConstantName", reference from: - [FileATest testMethod] in FileATest.o
In FileA.h
OBJC_EXTERN NSString *const ConstantName
In FileA.m
NSString *const ConstantName = #"ConstantValue"
In FileATest.m I use the constant and have the import
#import "FileA.h"
Using a static constant in the header file seems to work, but I read that it is not the proper way to declare a constant.
It was working fine with XCode 8 and previous versions, but for XCode 9 Beta 6 it doesn't. Has someone had this problem? How may I fix it?
I had the same issue when running unit tests and the solution for me was to go to the App Target > Build Settings > Dead Code Stripping and set it to NO.
Hope this helps.

iOS 11 - Xcode 9.0 Beta 3 error: Use of undeclared identifier 'vabs_s8'

Xcode gives me this weird error: Use of undeclared identifier 'vabs_s8'
File path: /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.0.sdk/usr/include/simd/common.h
This file is included by GameKit framework:
GameKit.h => simd.h => vector.h => common.h
Any suggestions how to fix this?
This error is to do with the architecture your targeting. A simple workaround is to place these at the top of common.h file.
#undef arm64
#undef arm
As you've noticed,gamekit includes simd/common.h in which ‘vabs_s8()’ function is undeclared.
Naturally, simd/common.h includes simd/base.h -> and -> simd/base.h includes arm_neon.h if ARM_NEON is defined. In arm_neon.h we have the declaration of ‘vabs_s8()’ function.
So if vabs_s8 is undeclared means the header is not included which means __ARM_NEON__ is not defined.
__ARM_NEON__ is enabled by default if you target armv7/arm64, so this means there might be something wrong with your project settings.
Solution: One of our programmers found out that adding "-mfpu=neon" in Other C Flags fixes this issue.

Duplicate symbols for architecture x86_64 under Xcode

I now have the same question with above title but have not found the right answer yet. I got the error:
/Users/nle/Library/Developer/Xcode/DerivedData/TestMoboSDK-Client-cgodalyxmwqzynaxfbbewrooymnq/Build/Intermediates/TestMoboSDK-Client.build/Debug-iphonesimulator/TestMoboSDK-Client.build/Objects-normal/x86_64/MoboSDK.o
/Users/nle/Library/Developer/Xcode/DerivedData/TestMoboSDK-Client-cgodalyxmwqzynaxfbbewrooymnq/Build/Products/Debug-iphonesimulator/libMoboSDK.a(MoboSDK.o)
duplicate symbol _OBJC_METACLASS_$_MoboSDK in:
/Users/nle/Library/Developer/Xcode/DerivedData/TestMoboSDK-Client-cgodalyxmwqzynaxfbbewrooymnq/Build/Intermediates/TestMoboSDK-Client.build/Debug-iphonesimulator/TestMoboSDK-Client.build/Objects-normal/x86_64/MoboSDK.o
/Users/nle/Library/Developer/Xcode/DerivedData/TestMoboSDK-Client-cgodalyxmwqzynaxfbbewrooymnq/Build/Products/Debug-iphonesimulator/libMoboSDK.a(MoboSDK.o)
ld: 75 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any help is appreciated.
Finally I find out the reason of this error cause I added -ObjC to the Other Linker Flags. After remove this value then I can build my project successfully, but I don't know why. Can anyone explain this?
For me, changing 'No Common Blocks' from Yes to No ( under Targets->Build Settings->Apple LLVM - Code Generation ) fixed the problem.
Stupid one, but make sure you haven't #imported a .m file by mistake somewhere
75 duplicate symbols for architecture x86_64
Means that you have loaded same functions twice.
As the issue disappear after removing -ObjC from Other Linker Flags,
this means that this option result that functions loads twice:
from Technical Q&A
This flag causes the linker to load every object file in the library
that defines an Objective-C class or category. While this option will
typically result in a larger executable (due to additional object code
loaded into the application), it will allow the successful creation of
effective Objective-C static libraries that contain categories on
existing classes.
https://developer.apple.com/library/content/qa/qa1490/_index.html
In my case, I just created a header file to define constant strings like this:
NSString *const AppDescriptionString = #"Healthy is the best way to keep fit";
I solved this scenario by using static:
static NSString *const AppDescriptionString = #"Healthy is the best way to keep fit";
Happens also when you declare const variables with same name in different class:
in file Message.m
const int kMessageLength = 36;
#implementation Message
#end
in file Chat.m
const int kMessageLength = 20;
#implementation Chat
#end
I have same problem.
In Xcode 7.2 in path Project Target > Build Setting > No Common Blocks, i change it to NO.
I found the accepted answer touches on the problem but didn't help me solve it, hopefully this answer will help with this very frustrating issue.
duplicate symbol _OBJC_IVAR_$_BLoginViewController._hud in:
17 duplicate symbols for architecture x86_64
"Means that you have loaded same functions twice. As the issue disappear after removing -ObjC from Other Linker Flags, this means that this option result that functions loads twice:"
In layman's terms this means we have two files in our project with exactly the same name. Maybe you are combining one project into another one? Have a look at the errors above the "duplicate symbols" error to see which folder is duplicated, in my case it was BLoginViewController.
For example, in the image below you can see I have two BImageViewControllers, for me this is what was causing the issue.
As soon as I deleted one then the issue vanished :)
This occurred on me when I accepted "recommended settings" pop-up on a project that I develop two years ago in Objective-C.
The problem was that when you accepted the "recommended settings" update, Xcode automatically changed or added some build settings, including GCC_NO_COMMON_BLOCKS = YES;.
This made the build failed with the duplicate symbol error in my updated project. So I changed No Common Block to NO in my build settings and the error was gone.
I experienced this issue after installing Cocoapods. Now happens everytime I update some pods. Solution I've found:
Go to terminal:
1) pod deintegrate
2) pod install
Also, check the item "Always Embed Swift Libraries" in your Build Settings. It should be "faded" indicating it is using the default configuration. If its set to a manual YES, hit delete over it to revert it to the default configuration. This stopped the behavior.
Fastest way to find the duplicate is:
Go to Targets
Go to Build Phases
Go to Compile Sources
Delete duplicate files.
Go to Targets
Select Build Settings
Search for "No Common Blocks", select it to NO.
It worked for me
Following steps solved the issue for me.
Go to Build Phases in Target settings.
Go to “Link Binary With Libraries”.
Check if any of the libraries exist twice.
Build again.
Remove -ObjC from Other Linker Flags or
Please check you imported any .m file instead of .h by mistake.
Update answer for 2021, Xcode 12.X:
pod deintegrate
pod install
Hope this helps!
My situation with some legacy project opened in Xcode 7.3 was:
duplicate symbol _SomeEnumState in:
followed with list of two unrelated files.o, then this was repeated couple of times, then finally:
ld: 8 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What solved it for me was changing enum declaration from:
enum SomeEnumState {
SomeEnumStateActive = 0,
SomeEnumStateUsed = 1,
SomeEnumStateHidden = 2
} SomeEnumState;
to this:
typedef NS_ENUM(NSUInteger, SomeEnumState) {
SomeEnumStateActive = 0,
SomeEnumStateUsed = 1,
SomeEnumStateHidden = 2
};
If somebody has explanation for this, please enlighten me.
Defining same variable under #implementation in more than one class also can cause this problem.
In my case, there were two file by same name in the location
Targets > Build Phases > Compile Sources and delete any duplicate files.
For me during the Xcode8 recommended project settings update "No Common Blocks" to YES which causes this issue.
My problem was that I had 5 duplicate symbols for architecture x86_64. After reading this post and their answers, I try with the common solution about change GCC_NO_COMMON_BLOCKS = YES to NO
But, instead of working for me, I went from 5 duplicates to 1 duplicate...
So, I paid attention to that last error, and I realized what was my problem, and it was an "incompatibility" with these packages (I had both in package.json):
rn-fetch-blob
react-native-blob-util
The message was clear about it, and I remove rn-fetch-blob because I have not idea why it was in my project, but, I only used with jest and delete it, it wasn't a problem.
So, after removing that package, and run yarn again, problem solved... And without changing the GCC_NO_COMMON_BLOCKS
Today , I got the same error . The error's key word is duplicate. I fix it by:
1. Remove the duplicate file at Build Phases-->Compile Sources
2. If you can not remove it at Build Phases, you need find the file at your project and remove the reference by DELETE :
3. Add the file to your project again
4. Add the file's .m to your Build Phases-->Compile Sources again
5. Build your project, the error will disappear
Another silly mistake that will cause this error is repeated files. I accidentally copied some files twice. First I went to Targets -> Build Phases -> Compile sources. There I noticed some files on that list twice and their locations.
Make sure you haven't imported a .m file by accident, you might want to delete your derived data in the Projects Window and then build and run again.
I got the same error when I added a pod repository
pod 'SWRevealViewController'
for an already added source code (SWRevealViewController) from gitHub. So, the error will be fixed by either removing the source code or pod repository.
Case # 2:
The 2nd time, this error appeared when I declare a constant in .h file.
NSString * const SomeConstant = #"SomeValue";
#interface AppDelegate : UIResponder <UIApplicationDelegate> {
...
...
None of above solutions works for me, I just fixed it myself.
I got duplicate symbol of my core data model which I make it myself, but
in my .xcdatamodeld inspector, I choose Class Definition of Codegen property, I guess that's what's wrong with. Then I choose Manual/None,it got fixed.
Hope this can be helpful for you!
For anyone else who is having this issue, I didn't see my resolution in any of these answers.
After having a .pbxproj merge conflict which was manually addressed (albeit poorly), there were duplicate references to individual class files in the .pbxproj. Deleting those from the Project > Build Phases > Compile Sources fixed everything for me.
Hope this helps someone down the line.
Similar to Juice007, I had declared and initialized a C type variable in two different .m files (that weren't imported!)
BOOL myVar = NO;
however, this method of declaring and initializing a variable, even in .m, even in #implementation grants it global scope. Your options are:
Declare it as static, to limit the scope to class:
static BOOL myVar = NO;
Remove the initialization (which will make the two classes share the global var):
BOOL myVar;
-(void) init{
myVar = NO;
}
Declare it as a property:
#property BOOL myVar;
Declare it as a proper iVar in the #interface
#interface myClass(){
BOOL myVar;
}
#end
In my case I had two main() methods defined in my project and removing one solved the issue.
The answers above didn't work for me. Here's how I got around it:
1) in finder, delete the entire Pods folder and Podfile.lock file
2) close the xcode project
3) run pod install in the terminal
4) open the xcode project, run the clean build command
Worked for me after that.
Because I haven't seen this answer:
Uninstall and reinstall your podfiles! Remove or uninstall library previously added : cocoapods
I've run into this issue over 3 times building my app and every time this is what fixes it. :)
I've just had this error as well. Found that the problem was variables declared with global scope, with the same names, were being repeated throughout the files being compiled into the program. Once changing the global variables to local scope to the pseudo-main function the error was resolved.

UIColor Category Linker Errors

Earlier today I tried to add Unit tests to my xcode project for a static framework that I am creating. After adding the Unit Tests target to my project, I was able to successfully run the unit tests template and have the tests catch the default assertion. Then, after importing the file that I wanted to test into my new SenTestCase subclass, I tried to run the test but my UIColor category failed building with the test due to linker errors. The text of the linker errors is as follows:
Undefined symbols for architecture i386:
"_CGColorGetComponents", referenced from:
-[UIColor(ColorExtension) hexValue] in StaticFramework(StaticFramework)
"_CGColorGetNumberOfComponents", referenced from:
-[UIColor(ColorExtension) hexValue] in StaticFramework(StaticFramework)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Next, I found where these two references were in my project. Both live in my category under the following function:
- (NSString *)hexValue
{
UIColor *color = self;
const size_t totalComponents = CGColorGetNumberOfComponents(color.CGColor);
const CGFloat * components = CGColorGetComponents(color.CGColor);
return [NSString stringWithFormat:#"#%02X%02X%02X",
(int)(255 * components[MIN(0,totalComponents-2)]),
(int)(255 * components[MIN(1,totalComponents-2)]),
(int)(255 * components[MIN(2,totalComponents-2)])];
}
If I remember correctly, both of these are part of the NSColor class. With a little more inspection, I also noticed that in my Unit Test's framework's folder, both UIKit.framework & Foundation.framework are red. I have tried reimporting them and building but this does not fix that issue either.
Interestingly enough, my static framework will still build and run fine as long as the associated Test is not also run. And when I comment this function out (and every time I use it), the Unit Test builds without issues. Would I be correct in my assumption that the missing UIKit.framework & Foundation.framework could be causing the linker not finding the NSColor properties? Does anyone have any additional suggestions that could explain why these properties are causing these issues or what I could do to fix them?
Any help would be greatly appreciated. Thanks for the assistance.
The linker error indicates that you are using CoreGraphics functions but you are not linking the CoreGraphics framework.
Update your project/target so you link the CoreGraphics.framework and the linker issues will be resolved.

how to link a static library for iOS

I have create a bunch of .o files (via gcc -c $file.c $someotherops -o $file.o). Now I want to link them into a static library.
I'm not exactly sure wether I am supposed to use ld or gcc for this. In the ld manual, it is said that I'm not supposed to use it directly. However, I cannot figure out the gcc parameters to create a static library.
I tried ld *.o -static -o libfoo.a but it complains about a lot of missing symbols (I think all from libc). I don't understand why it complains because it is supposed to be a static library. I thought it would check for the symbols once I link that static library to some other thing.
Another thing: I use /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ld here (my target is iOS). It complains with the warning ld: warning: using ld_classic. What is this about?
Then I thought, maybe it needs to have the dynamic libraries specified. So I added -lc to link against libc. But it complains with can't locate file for: -lc. I added -L/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/usr/lib and there is a libc.dylib.
Any ideas?
About the -lc error: It got away after I specified -arch armv6. Then it complained about a wrong libcache.dylib (which must be linked from libc.dylib I guess because it didn't specified it). Adding -L.../usr/lib/system helped.
Now, for each single .o file, I get the warning ld: warning: CPU_SUBTYPE_ARM_ALL subtype is deprecated. What is this about?
And I still have a bunch of missing symbols, esp:
Undefined symbols for architecture armv6:
"start", referenced from:
-u command line option
(maybe you meant: _PyThread_start_new_thread)
"___udivsi3", referenced from:
_get_len_of_range in bltinmodule.o
_quorem in dtoa.o
_array_resize in arraymodule.o
_newarrayobject in arraymodule.o
_array_fromfile in arraymodule.o
_get_len_of_range in rangeobject.o
_inplace_divrem1 in longobject.o
...
"___unorddf2", referenced from:
_builtin_round in bltinmodule.o
...
I checked some of those symbols, e.g. ___udivsi3 in get_len_of_range. This function uses C arithmetic only, no external call. So this seems to be translated to use some external functions like ___udivsi3. But what libraries is this in?
-lgcc_s.1 fixed most of the ___udivsi3 and related missing symbols. The start symbol is still missing. What does -u command line option mean?
From here, I got the feeling that maybe ld isn't the right tool after all. There, a simple call to aris used. And this seem to make more sense. I will check if that does work and transform this into an answer then.
While playing more around, ar throw some warnings at me when building a fat static library. It gave me the hint to use libtool instead. That is what I'm doing now, i.e. libtool -static -o libfoo.a *.o. Also I switched the compiler to /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clangbut not sure if that matters.
Now, at compiling some test application which links to this static library, I get these warnings:
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not allowed in code signed PIE, but used in __PyBuiltin_Init from /Users/az/Programmierung/python-embedded/libpython.a(bltinmodule.o). To fix this warning, don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
ld: warning: 32-bit absolute address out of range (0x1001B70C4 max is 4GB): from _usedpools + 0x00000004 (0x001B70CC) to 0x1001B70C4
ld: warning: 32-bit absolute address out of range (0x1001B70C4 max is 4GB): from _usedpools + 0x00000000 (0x001B70CC) to 0x1001B70C4
What are they about? I don't use -mdynamic-no-pic. I also don't really see in _PyBuiltin_Init how I use absolute addressing there.
Also, what are these absolute addresses out of range about? Edit: These were some really huge allocations. I just removed this code for now (this was WITH_PYMALLOC, if anyone is interested in these specific Python internals).
When I start it on my iPhone, I get the abort:
dyld: vm_protect(0x00001000, 0x00173000, false, 0x07) failed, result=2 for segment __TEXT in /var/mobile/Applications/C15D9525-E7DC-4463-B05B-D39C9CA24319/...
When I use -no_pie for linking, it doesn't even link. It fails with:
Illegal text-relocation to ___stderrp in /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/usr/lib/libSystem.dylib from _read_object in /Users/az/Programmierung/python-embedded/libpython.a(marshal.o) for architecture armv7
I solved the PIE disabled, Absolute addressing error. I had a -static in my command line Clang. Once I removed that, the warning got away, as well as the dyld/vm_protect error. It was the first time it actually run some code.
Until I hit another strange error about integer comparison. Whereby this looks more like a bug in their Clang build.
It all works now. Basically, the answer is:
Just compile every *.c file as usual to *.o files. The only real difference is the different GCC/Clang, the -arch armv7, the different SDK / include dirs.
Use libtool -static -o libfoo.a *.o to build the static library.
That's it. The other problems in my question were just wrongly headed tries.
If anyone gets here by searching for the dyld: vm_protect(...) runtime error but you are using XCode, the -static flag mentioned in the OP is likely the issue.
Get rid of it by switching "Enable linking with shared libraries" to "Yes" (the default) in the LLVM compiler language settings. (This removes GCC_LINK_WITH_DYNAMIC_LIBRARIES = NO from the project file).

Resources