macでc/c++をコンパイルできない。
macで下記C/C++のコードを書きましたが、下記エラーでコンパイルできません。
intarray.cpp
#include<stddef.h>
#include<stdlib.h>
#include<stdio.h>
struct IntArray {
int *elems;
size_t numElems;
};
void IA_init(IntArray *object);
void IA_cleanup(IntArray *object);
void IA_setSize(IntArray *object, size_t value);
size_t IA_getSize(IntArray *object);
void IA_setElem(IntArray *object, size_t index, int value);
int IA_getElem(IntArray *object, size_t index);
void IA_init(IntArray *object, size_t value){
object->numElems = 0;
object->elems = 0;
}
void IA_cleanup(IntArray *object){
free(object->elems);
}
void IA_setSize(IntArray *object, size_t value){
if(object->elems != 0) free(object->elems);
object->numElems = value;
object->elems = (int *)malloc(value * sizeof(int));
}
void IA_setElem(IntArray *object, size_t value, int index){
if(index >= object->numElems) {
printf("bad index");
return;
}
object->elems[index] = value;
}
int IA_getElem(IntArray *object, size_t index){
if(index >= object->numElems){
printf("bad index");
return 0;
}
return object->elems[index];
}
int main(){
IntArray powersOf2;
IA_init(&powersOf2);
IA_setSize(&powersOf2, 8);
IA_setElem(&powersOf2, 0, 1);
int i;
for(i=1; i<8; i++){
IA_setElem(&powersOf2, i, 2 * IA_getElem(&powersOf2, i-1));
}
IA_cleanup(&powersOf2);
return 0;
}
コマンド
g++ -v intarray.cpp -o intarray
エラー
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name intarray.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 253.9 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/username/workspaces/c_plus_plus_workspace/class -ferror-limit 19 -fmessage-length 144 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/2z/x8jb59gs1lz7n1hhbhk_st8c0000gn/T/intarray-f42434.o -x c++ intarray.cpp
clang -cc1 version 7.0.2 based upon LLVM 3.7.0svn default target x86_64-apple-darwin15.2.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 -o intarray /var/folders/2z/x8jb59gs1lz7n1hhbhk_st8c0000gn/T/intarray-f42434.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"IA_init(IntArray*)", referenced from:
_main in intarray-f42434.o
(maybe you meant: __Z7IA_initP8IntArraym)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
検索すると幾つか同じエラーになっているという報告を見られましたが、解決方法を試しても解決しませんでした。
しかし、下記コードはコンパイルできました。
hello.cpp
#include<stdio.h>
int main(){
printf("Hello, world");
return 0;
}
hello.cpp
#include<iostream>
int main(){
std::cout << "Hello, world" << "\n";
return 0;
}
なぜ上記のコードはコンパイルできないのでしょうか。
実行環境
Mac El Captan bash
追記
ちなみに下記コードでもエラーが出ました。
intarray2.h
#include<stddef.h>
#include<stdio.h>
#include<stdlib.h>
class IntArray{
public:
void init();
void cleanup();
void setSize(size_t value);
size_t getSize();
void setElem(size_t index, int value);
int getElem(size_t index);
private:
int *elems;
size_t numElems;
};
intarray2.cpp
#include<stddef.h>
#include<stdio.h>
#include<stdlib.h>
class IntArray{
void IntArray::init(){
this->numElems = 0;
this->elems = 0;
}
void IntArray::cleanup(){
this->numElems = 0;
free(this->elems);
}
void setSize(size_t, value){
if(this->elems != 0) free(this->elems);
this->numElems = value;
this->elems = (int *) malloc(value * sizeof(int));
}
void IntArray::setElem(size_t valuem, int index){
if(index >= this->numElems){
printf("bad index");
return;
}
this->elems[index] = value;
}
int IntArray::getElem(size_t index){
if(index >= this->numElems){
printf("bad index");
return 1;
}
return this->elems[index];
}
}
main.cpp
#include "IntArray2.h"
int main(){
IntArray powerOf2;
powerOf2.init();
powerOf2.setSize(8);
int i;
for(i=0;i<8;i++){
powerOf2.setElem(i, 2 * powerOf2.getElem(i-1));
}
powerOf2.cleanup();
return 0;
}
エラー
Undefined symbols for architecture x86_64:
"IntArray::init()", referenced from:
_main in main-d429d9.o
"IntArray::cleanup()", referenced from:
_main in main-d429d9.o
"IntArray::getElem(unsigned long)", referenced from:
_main in main-d429d9.o
"IntArray::setElem(unsigned long, int)", referenced from:
_main in main-d429d9.o
"IntArray::setSize(unsigned long)", referenced from:
_main in main-d429d9.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)