2016-10-26 6 views
1

私は動的ライブラリを作成しています。ライブラリ内で未定義のextern変数を使用すると、macOSでリンカエラーが発生する

foo.hという

extern unsigned int myoperator; 
int operate(int a, int b); 

foo.cpp

#include "foo.h" 
int operate(int a, int b){ 
    switch(myoperator){ 
    case 0: 
     return a+b; 
    case 1: 
     return a-b; 
    default: 
     return a*b; 
    } 
} 

libfooしかしながら、そのは、MacOSの打ち鳴らすにリンカーエラーをスロー、LinuxのGCCのC++ 14に非常によく構築されていますC++ 14。エラーが

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

私はグーグルに乗った問題に最も近いリンクは、私は答えはそこにあるかどうかわからないのですhttps://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/DynamicLibraryDesignGuidelines.html

です。

+1

1. 'foo.cpp'で定義する必要があります。 2.保存されている名前 'operator'を使用しないでください。 – songyuanyao

+0

私が使用した正確なコードではありません。私の実際のコードでは、私は 'operator'を使用しませんでした。ありがとうございました – neckTwi

+0

@songyuanyaoもし 'foo.cpp'で定義すれば、クライアントで再定義することはできません。上記のコードはlinux gccに組み込まれています – neckTwi

答えて

0

「myoperator」変数の「本物」の定義は、同じプロジェクトfooの一部である別のファイルにある場合に属し、私はあなたが行のコードで

extern unsigned int myoperator; 

を置けば、それは良いはずだと思いますあなたのcppファイル

関連する問題