problem:
i cannot code provided author link via clang++ in chapter 8 of programming principles , practice bjarne stroustrup.
code:
~/scratch/cpp/chp8 (09/10/2017-13:51:43[edt]) cat my.cpp #include <iostream> #include "my.h" void print_foo() { std::cout << foo << std::endl; } void print(int i) { std::cout << << std::endl; } ~/scratch/cpp/chp8 (09/10/2017-13:52:29[edt]) cat my.h #ifndef my_h #define my_h extern int foo; void print_foo(); void print(int); #endif ~/scratch/cpp/chp8 (09/10/2017-13:52:33[edt]) cat use.cpp #include "my.h" int main() { foo = 7; print_foo(); print(99); return 0; }
attempts:
i have tried few different ways of compiling this:
one - compile driver app contains main function.
~/scratch/cpp/chp8 (09/10/2017-13:52:39[edt]) clang++ -std=c++14 -stdlib=libc++ use.cpp -o use.cpp.o undefined symbols architecture x86_64: "print(int)", referenced from: _main in use-2864c4.o "print_foo()", referenced from: _main in use-2864c4.o "_foo", referenced from: _main in use-2864c4.o ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation)
two - compile my.cpp file , not link. although successful in compiling i'm not sure how link use.cpp object file now.
~/scratch/cpp/chp8 (09/10/2017-14:09:40[edt]) clang++ -std=c++14 -stdlib=libc++ -c my.cpp -o my.cpp.o
three - use .cpp files.
~/scratch/cpp/chp8 (09/10/2017-14:15:32[edt]) clang++ -std=c++14 -stdlib=libc++ my.cpp use.cpp undefined symbols architecture x86_64: "_foo", referenced from: print_foo() in my-2243d1.o _main in use-796f91.o (maybe meant: __z9print_foov) ld: symbol(s) not found architecture x86_64 clang: error: linker command failed exit code 1 (use -v see invocation)
question:
besides obvious question of doing wrong, i'm more interested in missing or miss understanding here?
note:
i'm not looking personal opinions or preferences on should or should not doing. purely education adventure i'm taking book , keep such.
misc:
~/scratch/cpp/chp8 (09/10/2017-14:15:25[edt]) uname -a darwin abes-macbook-pro.local 16.7.0 darwin kernel version 16.7.0: thu jun 15 17:36:27 pdt 2017; root:xnu-3789.70.16~2/release_x86_64 x86_64 ~/scratch/cpp/chp8 (09/10/2017-14:15:28[edt]) clang++ -v apple llvm version 8.1.0 (clang-802.0.42) target: x86_64-apple-darwin16.7.0 thread model: posix installeddir: /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin
the error has nothing difference between compiling , linking. rather there error in code. reason error because declaring global variable foo
never define it. need add line
int foo = 0;
to 1 of .cpp files. makes sense in my.cpp
.
note using global variables considered coding horror. should not in real code.
regarding compiling , linking:
creating executable c++ takes several steps. here talk compiling versus linking , how each of these steps manually. there other steps, not them here.
first of all, can compile , link of source files @ once:
$ clang++ -std=c++14 -stdlib=libc++ my.cpp use.cpp
i'm confused why not working in "attempt three". more shortly.
to compile , link source code manually, can following:
$ clang++ -std=c++14 -stdlib=libc++ -c my.cpp -o my.o $ clang++ -std=c++14 -stdlib=libc++ -c use.cpp -o use.o $ clang++ -std=c++14 -stdlib=libc++ my.o use.o -o use
note compile my.cpp
my.o
rather my.cpp.o
. common convention this.
also, once comfortable these concepts, suggest use build tool such make
or xcode project manages these steps automatically.
Comments
Post a Comment