Last active
December 12, 2015 06:28
-
-
Save akirakiron/4729600 to your computer and use it in GitHub Desktop.
C++で静的ライブラリをリンクする時の引数の順番で詰まったのでメモ.
ライブラリ間に依存関係がある場合には,依存している方を前に,依存されている方を後に置く.
そうしないと'undefined reference'エラーになる. arコマンドで一つのライブラリにまとめる際は,引数の順番は気にする必要はない.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int get_zero(); | |
int get_one() { | |
return get_zero() + 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g++ -c one.cpp -o one.o | |
g++ -c zero.cpp -o zero.o | |
ar rv zero zero.o | |
ar rv one one.o | |
g++ test.cpp -o test one zero # => ok | |
g++ test.cpp -o test zero one # => 'undefined reference 'get_zero()' | |
ar rv num one.o zero.o | |
g++ test.cpp -o test num # => ok | |
rm num | |
ar rv num zero.o one.o | |
g++ test.cpp -o test libnum.a # => ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int get_one(); | |
int main() { | |
std::cout << get_one() << std::endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int get_zero() { | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment