0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

最適化しないとコンパイルが通らないコード

Last updated at Posted at 2020-06-09

私の環境(Windows 7, MinGW64 g++ 8.1.0)で-O1, -O2ではコンパイルが通るのに-O0では通らないコードを、見つけてしまいました(エラーメッセージ後述)
ところでいい加減 Windows 7 使うのやめた方がいい ? 私もそう思います

# include <iostream>

struct A {
	void fuga() const {
		std::cerr << "fuga~~" << std::endl;
	}
};
struct B {
	static const A a;
	void hoge() {
		a.fuga();
	}
};
int main() {
	B a;
	a.hoge();
	return 0;
}

-O0でのエラーメッセージ

C:\...\cc15Jtni.o:b.cpp:(.rdata$.refptr._ZN1B1aE[.refptr._ZN1B1aE]+0x0): undefined reference to `B::a'
collect2.exe: error: ld returned 1 exit status

本当はダメだけど最適化によってリンク直前のコードにおけるstatic const A aへの参照が消え失せたのかな
「最適化オプション変えたら***が起きた」シリーズとして...

0
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?