AMI amitekh
AMI amitekh

Reputation: 198

Linker Command Failed with exit code 1 (Use -v to see invocation) iOS Error

In my application I'm using a static library named ABC.a in that library in a c file named Layout.c there is a function called init(). And I linked the library to the projects and added the .h file. The program is compiled without error but while linking the function its throwing the error. Why?

Info: I've added that static library in build phases also.

And the library is built for armv7, armv7s and arm64. bitcode enabled: No and Build active architectures : NO

Example error:

Undefined symbols for architecture arm64:
  "AMID_INIT(int*, int*, int)", referenced from:
      -[ViewController microphone:hasAudioReceived:withBufferSize:withNumberOfChannels:] in Test_lto.o
  "amid_Val(float const*, int, int*, int, unsigned int)", referenced from:
      -[ViewController microphone:hasAudioReceived:withBufferSize:withNumberOfChannels:] in Test_lto.o

Please help two days gone for this.

Upvotes: 0

Views: 1002

Answers (1)

Anya Shenanigans
Anya Shenanigans

Reputation: 94614

This is based on the fact that you mention that the .a file is generated from a c file. The linker error:

  "AMID_INIT(int*, int*, int)", referenced from:
      -[ViewController microphone:hasAudioReceived:withBufferSize:withNumberOfChannels:] in Test_lto.o

indicates that the AMID_INIT definition came from a C++/Objective-C++ file - this is because C files would not have information about the parameters of the routine.

From this I was able to surmise that the library header file did not have c++ guards.

Three approaches in this case - wrap all imports of the library header file in the C++ code with something like:

extern "C" {
#import "lib.h"
}

or create a lib.hpp file, containing:

#pragma once
extern "C" {
#import "lib.h"
}

and #import 'lib.hpp' instead, or fix the lib.h file by adding the standard name mangling preventative:

… near start of lib.h:

#ifdef __cplusplus
extern "C" {
#endif

… near end of lib.h:

#ifdef __cplusplus
}
#endif

This allows you to keep using the lib.h with both C and C++ compilers by declaring that all the routines offered by lib.h are exposed using C linkage, rather than C++ linkage.

Upvotes: 2

Related Questions