Last active
April 24, 2017 04:09
-
-
Save s-shin/2f477846fb805e95de11dea9ebbfb65e to your computer and use it in GitHub Desktop.
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 "foo.h" | |
int get_foo_a() { return foo; } |
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
#ifndef A_H_ | |
#define A_H_ | |
int get_foo_a(); | |
#endif // A_H_ |
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 "foo.h" | |
int get_foo_b() { return foo; } |
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
#ifndef B_H_ | |
#define B_H_ | |
int get_foo_b(); | |
#endif // B_H_ |
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 "foo.h" | |
#ifdef FOO_VALUE_IN_IMPL | |
#ifdef FOO_CONST | |
const | |
#endif | |
int foo = 100; | |
#endif |
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
#ifndef FOO_H_ | |
#define FOO_H_ | |
#ifdef FOO_EXTERN | |
extern | |
#endif | |
#ifdef FOO_CONST | |
const | |
#endif | |
int foo | |
#ifdef FOO_VALUE_IN_HEADER | |
= 100 | |
#endif | |
; | |
#endif // FOO_H_ |
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> | |
#include "a.h" | |
#include "b.h" | |
int main(int argc, char** argv) { | |
std::cout << "a: " << get_foo_a() << ", B: " << get_foo_b() << 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
$ bash test.sh | |
======= WITH VALUE IN HEADER ======= | |
duplicate symbol _foo in: | |
/var/folders/hl/l1bbbjnn5rndfvt9v5t73br82fc5dd/T/a-dfaee4.o | |
/var/folders/hl/l1bbbjnn5rndfvt9v5t73br82fc5dd/T/foo-6afa08.o | |
duplicate symbol _foo in: | |
/var/folders/hl/l1bbbjnn5rndfvt9v5t73br82fc5dd/T/a-dfaee4.o | |
/var/folders/hl/l1bbbjnn5rndfvt9v5t73br82fc5dd/T/b-210a48.o | |
ld: 2 duplicate symbols for architecture x86_64 | |
clang: error: linker command failed with exit code 1 (use -v to see invocation) | |
======= CONST WITH VALUE IN HEADER ======= | |
Result: a: 100, B: 100 | |
======= WITHOUT VALUE IN HEADER ======= | |
duplicate symbol _foo in: | |
/var/folders/hl/l1bbbjnn5rndfvt9v5t73br82fc5dd/T/a-15bab8.o | |
/var/folders/hl/l1bbbjnn5rndfvt9v5t73br82fc5dd/T/b-b7a30b.o | |
duplicate symbol _foo in: | |
/var/folders/hl/l1bbbjnn5rndfvt9v5t73br82fc5dd/T/a-15bab8.o | |
/var/folders/hl/l1bbbjnn5rndfvt9v5t73br82fc5dd/T/foo-8b4caf.o | |
ld: 2 duplicate symbols for architecture x86_64 | |
clang: error: linker command failed with exit code 1 (use -v to see invocation) | |
======= CONST WITHOUT VALUE IN HEADER ======= | |
In file included from a.cpp:1: | |
./foo.h:10:5: error: default initialization of an object of const type 'const int' | |
int foo | |
^ | |
= 0 | |
1 error generated. | |
In file included from b.cpp:1: | |
./foo.h:10:5: error: default initialization of an object of const type 'const int' | |
int foo | |
^ | |
= 0 | |
1 error generated. | |
In file included from foo.cpp:1: | |
./foo.h:10:5: error: default initialization of an object of const type 'const int' | |
int foo | |
^ | |
= 0 | |
1 error generated. | |
======= EXTERN WITHOUT VALUE IN HEADER AND VALUE IN IMPL ======= | |
Result: a: 100, B: 100 | |
======= EXTERN CONST WITHOUT VALUE IN HEADER AND VALUE IN IMPL ======= | |
Result: a: 100, B: 100 |
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
#!/bin/bash | |
set -eu | |
: ${BIN:=./a.out} | |
rm -f "$BIN" | |
echo '======= WITH VALUE IN HEADER =======' | |
if clang++ -std=c++11 -stdlib=libc++ a.cpp b.cpp foo.cpp main.cpp -DFOO_VALUE_IN_HEADER; then | |
echo -n 'Result: ' | |
"$BIN" | |
rm -f "$BIN" | |
fi | |
echo '======= CONST WITH VALUE IN HEADER =======' | |
if clang++ -std=c++11 -stdlib=libc++ a.cpp b.cpp foo.cpp main.cpp -DFOO_CONST -DFOO_VALUE_IN_HEADER; then | |
echo -n 'Result: ' | |
"$BIN" | |
rm -f "$BIN" | |
fi | |
echo '======= WITHOUT VALUE IN HEADER =======' | |
if clang++ -std=c++11 -stdlib=libc++ a.cpp b.cpp foo.cpp main.cpp; then | |
echo -n 'Result: ' | |
"$BIN" | |
rm -f "$BIN" | |
fi | |
echo '======= CONST WITHOUT VALUE IN HEADER =======' | |
if clang++ -std=c++11 -stdlib=libc++ a.cpp b.cpp foo.cpp main.cpp -DFOO_CONST; then | |
echo -n 'Result: ' | |
"$BIN" | |
rm -f "$BIN" | |
fi | |
echo '======= EXTERN WITHOUT VALUE IN HEADER AND VALUE IN IMPL =======' | |
if clang++ -std=c++11 -stdlib=libc++ a.cpp b.cpp foo.cpp main.cpp -DFOO_EXTERN -DFOO_VALUE_IN_IMPL; then | |
echo -n 'Result: ' | |
"$BIN" | |
rm -f "$BIN" | |
fi | |
echo '======= EXTERN CONST WITHOUT VALUE IN HEADER AND VALUE IN IMPL =======' | |
if clang++ -std=c++11 -stdlib=libc++ a.cpp b.cpp foo.cpp main.cpp -DFOO_EXTERN -DFOO_VALUE_IN_IMPL -DFOO_CONST; then | |
echo -n 'Result: ' | |
"$BIN" | |
rm -f "$BIN" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment