(C++) Oject file MyFile.OBJ is missing a section of class 2

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) Oject file MyFile.OBJ is missing a section of class 2

 

Link error.

 

IDE: C++ Builder 6.0

Project type: Console

 

 

 

 

 

Full error messages

 


[Linker Fatal Error] Fatal: Oject file MyFile.OBJ is missing a section of class 2

 

 

 

 

 

Cause

 

This error occurs when:

 

Download a C++ Builder project with this error.

 

 

 

 

 

Solutions

 

There are more.

 

 

 

 

 

 

Restructure the program so you will not need to use globals. If this fails, use the Singleton Design Pattern.

 

 

 

 

 

 

Go to 'Project | Options | Compiler | Pre-compiled headers' and select 'None'. Your error message will change to the following warning:

 


[Linker Warning] Public symbol '_x' defined in both module MyUnit1.OBJ and MyUnit2.OBJ [Linker Warning] Public symbol '_x' defined in both module MyUnit1.OBJ and MyUnit3.OBJ

 

This solution is not recommended, because one should not use global data [1-5], nor work in the global namespace [6].

 

 

 

 

 

 

Suppose you have declared an integer called 'x' in a header file (.h) like below:

 


int x;

 

Put it into a namespace called 'global' like below:

 


namespace global {   int x; }

 

 

 

 

 

Your error message will change to the following warning:

 


[Linker Warning] Public symbol 'global::x' defined in both module MyUnit1.OBJ and MyUnit2.OBJ [Linker Warning] Public symbol 'global::x' defined in both module MyUnit1.OBJ and MyUnit3.OBJ

 

This solution is not recommended, because one should not use global data [1-5], nor work in the global namespace [6].

 

 

 

 

 

Suppose you have declared an integer called 'x' in a header file (.h) like below:

 


int x;

 

Make it static like below:

 


static int x;

 

 

 

 

 

This solution is not recommended, because one should not use global data [1-5], nor work in the global namespace [6].

 

 

 

 

 

References

  1. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 10: 'Minimize global and shared data'
  2. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Item 18: 'Declare variables as locally as possible'
  3. Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Chapter 1.8.2.a: 'Don't use global data (use members)'
  4. Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. ISBN: 0-672-32480-6. Chapter 3: 'Avoid using global variables'
  5. Jesse Liberty. Sams teach yourself C++ in 24 hours. ISBN: 0-672-32224-2. Hour 5, paragraph 'Global variables': 'In C++, global variables are avoided because they can create very confusing code that is hard to maintain.'
  6. Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4. Chapter C.14.15: 'Don't pollute the global namespace'