Skip to content

Instantly share code, notes, and snippets.

@davidberard98
Last active July 6, 2022 00:35
Show Gist options
  • Save davidberard98/716679a9bae7ba76f37a224943cdcdf1 to your computer and use it in GitHub Desktop.
Save davidberard98/716679a9bae7ba76f37a224943cdcdf1 to your computer and use it in GitHub Desktop.
duplicate build
g++ counter.cpp -c -o counter.o
g++ x.cpp -c -o x.o
g++ x.cpp -c -o y.o
g++ main.cpp -c -o main.o
g++ counter.o x.o y.o main.o -o main
#include "counter.h"
int& counter() {
static int cnt = 0;
return cnt;
}
CounterIncr::CounterIncr() {
counter() += 1;
}
int& counter();
struct CounterIncr {
CounterIncr();
};
#include <iostream>
#include "counter.h"
int main() {
std::cout << "Counter: " << counter() << std::endl;
return 0;
}
#include <iostream>
#include "counter.h"
namespace {
CounterIncr ci;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment