0

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.

3
  • 1
    At a guess, the headers for the library don't have C++ guards and you're using them in an objective-C++ program. try wrapping the #import 'lib.h' in an extern "C" {\n#import 'lib.h'\n} to see if that helps things. Commented May 23, 2017 at 8:28
  • try this : If your Architectures and Valid Architectures are all right, you may check whether you have add $(inherited) to Other Linker Flags. Commented May 23, 2017 at 8:33
  • @Petesh Your comment is absolutely correct. Awesome man. You saved a lot of my time. Thanks. Put an answer I will check it up. Commented May 23, 2017 at 9:12

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.