Search code examples
iosobjective-ccocoapodsobjective-c++

"pod lib lint" command throws error with mixed objective-c and objective-c++ files


I try to create a simple cocoapods library with objective-c and objective-c++ files. When executing the command pod lib lint I get an error.

Pod structure:

  • mypod

    • src

      • // A.h
        #pragma once
        
        #import <Foundation/Foundation.h>
        
        NSString* foo(void);
        NSString* bar(void);
        
      • // A.m
        #import "foo.h"
        #import "bar.h"
        
        NSString* foo() {
          return @"Foo";
        }
        
        NSString* bar() {
          int myInt = getValue();
          return [NSString stringWithFormat:@"%d",myInt];
        }
        
        
      • // B.h
        #pragma once
        
        int getValue(void);
        
      • // B.mm
        #import "bar.h"
        
        int getValue(void)
        {
          return 2;
        }
        
    • mypod.podspec

      Pod::Spec.new do |spec|
          spec.name                     = 'mypod'
          spec.version                  = '1.0'
          spec.homepage                 = 'mypod'
          spec.source                   = { :git => "Not Published", :tag => "Cocoapods/#{spec.name}/#{spec.version}" }
          spec.authors                  = ''
          spec.license                  = ''
          spec.summary                  = 'mypod'
          spec.ios.deployment_target    = '13.5'
          spec.source_files             = 'src/A.h', 'src/A.m', 'src/B.h', 'src/B.mm'
          spec.private_header_files     = 'src/A.h'
          spec.public_header_files      = 'src/B.h'
          spec.library = "c++"
          spec.pod_target_xcconfig      = {
          "OTHER_LDFLAGS" => "-lstdc++",
          'LANG_CXX_LANGUAGE_STANDARD' => 'c++20',
          'CLANG_CXX_LIBRARY' => 'libc++',
          'ARCHS' => 'arm64'
          }
      end
      

Executing the command `pod lib lint --platforms=ios --verbose` on an M1 mac throws an error:

    Undefined symbols for architecture arm64:
      "_getValue", referenced from:
          _bar in foo.o
    ld: symbol(s) not found for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    ** BUILD FAILED **

Does somebody know what's the problem?


Solution

  • When importing the sames headers into C++ and Objective-C compilation units, you have to wrap the declarations with extern "C" guards to avoid their names being mangled when linking:

    #ifdef __cplusplus
    extern "C" {
    #endif
    
    int getValue(void);
    
    #ifdef __cplusplus
    }
    #endif