C++ odr用法案例詳解
// The main module. File: odr_test1.cpp #include <iostream> void module1_print(); // declaration of an exeternal function inline int f1() { return 4; } class A { public: static double f() { return 4.1; } }; const double C = 4.2; constexpr double E = 4.5; void print() { std::cout << "main f1(): " << f1() << std::endl; std::cout << "main A::f(): " << A::f() << std::endl; std::cout << "main C: " << C << std::endl; std::cout << "main E: " << E << std::endl; } int main() { module1_print(); print(); int i; std::cin >> i; }
// File: module1.cpp #include <iostream> inline int f1() { return 3; } class A { public: static double f() { return 3.1; } }; const double C = 3.2; constexpr double E = 3.5; void module1_print() { std::cout << "module1 f1(): " << f1() << std::endl; std::cout << "module1 A::f(): " << A::f() << std::endl; std::cout << "module1 C: " << C << std::endl; std::cout << "module1 E: " << E << std::endl; }
1、在VS2017上運(yùn)行的結(jié)果為:
2、使用clang進(jìn)行編譯
clang++ module1.cpp odr_test1.cpp
運(yùn)行結(jié)果:
若進(jìn)行下面的編譯:
clang++ odr_test1.cpp module1.cpp
則結(jié)果如下
3、使用gcc編譯
g++ module1.cpp odr_test1.cpp -std=c++11
若進(jìn)行如下編譯
g++ odr_test1.cpp module1.cpp -std=c++11
二、如何解決這個(gè)問題
// The main module. File: odr_test2.cpp #include <iostream> void module2_print(); // declaration of an external function namespace { inline int f1() { return 4; } class A { public: static double f() { return 4.1; } }; } const double C = 4.2; constexpr double E = 4.5; void print() { std::cout << "main f1(): " << f1() << std::endl; std::cout << "main A::f(): " << A::f() << std::endl; std::cout << "main C: " << C << std::endl; std::cout << "main E: " << E << std::endl; } int main() { module2_print(); print(); int i; std::cin >> i; }
// File: module2.cpp #include <iostream> namespace { inline int f1() { return 3; } class A { public: static double f() { return 3.1; } }; } const double C = 3.2; constexpr double E = 3.5; void module2_print() { std::cout << "module2 f1(): " << f1() << std::endl; std::cout << "module2 A::f(): " << A::f() << std::endl; std::cout << "module2 C: " << C << std::endl; std::cout << "module2 E: " << E << std::endl; }
運(yùn)行結(jié)果
到此這篇關(guān)于C++ odr用法案例詳解的文章就介紹到這了,更多相關(guān)C++ odr用法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于OpenCv的運(yùn)動(dòng)物體檢測(cè)算法
這篇文章主要為大家詳細(xì)介紹了基于OpenCv的運(yùn)動(dòng)物體檢測(cè)算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01C++ 學(xué)習(xí)之旅三 我和超級(jí)瑪麗有個(gè)約會(huì)
學(xué)習(xí)了c++有一周有余了吧,感謝孫鑫老師的視頻教程,讓我 對(duì)C++有了基本的了解,并理解到C++與.net 的許許多多的區(qū)別,更要感謝網(wǎng)民為programaking的人,會(huì)為我提供了超級(jí)瑪麗制作揭秘 這套寶貴的教程,讓我 做做出了這個(gè)項(xiàng)目,對(duì)c++ 有了一個(gè)更深層次的認(rèn)識(shí)2012-11-11用C語(yǔ)言實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了用C語(yǔ)言實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10詳解約瑟夫環(huán)問題及其相關(guān)的C語(yǔ)言算法實(shí)現(xiàn)
這篇文章主要介紹了詳解約瑟夫環(huán)問題及其相關(guān)的C語(yǔ)言算法實(shí)現(xiàn),也是ACM當(dāng)中經(jīng)常會(huì)引用到的基礎(chǔ)題目,文中共介紹了三種C語(yǔ)言解答,需要的朋友可以參考下2015-08-08你不知道的C++中namespace和using的用法實(shí)例
在C++語(yǔ)言編寫的程序中,變量和函數(shù)等的作用范圍是有一定限制的,下面這篇文章主要給大家介紹了一些你不知道的C++中namespace和using的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12