1

so I work in an environment where we are not able to link any runtime libraries, or use exceptions. However we want to take advantage of either BOOST or STL. Initially we tried linking in boost::container::string/vector/map/... and were unable to resolve linker errors.

So currently I am trying to link in STL's headers, to do so I defined the following preprocessor directives:

#define _HAS_ITERATOR_DEBUGGING 0

#define _HAS_EXCEPTIONS 0

I am able to link succesfully with #include and I am able to instantiate a std::vector, however when I call push_back(32); on the vector I am greeted with the following linker errors:

error LNK2001: unresolved external symbol "void (__cdecl* std::_Raise_handler)(class stdext::exception const &)" (?_Raise_handler@std@@3P6AXAEBVexception@stdext@@@ZEA)
error LNK2001: unresolved external symbol _invoke_watson
error LNK2001: unresolved external symbol "void __cdecl std::_Xlength_error(char const *)" (?_Xlength_error@std@@YAXPEBD@Z)

we are using visual studio 2010 (and it's default compiler). We are looking to migrate to vs2012 in the near future, which could be expedited if it helped resolve these errors.

Is there any way to link either boost or STL under these restrictions (no exceptions, no runtime libraries)?

Thanks! We really don't want to miss out on the benefits of STL / boost because of our environment restrictions

Short
  • 7,767
  • 2
  • 25
  • 33
  • 4
    what on earth environment are you in!@?!?!??@! – Wug Sep 13 '12 at 19:39
  • When you say runtime library do you mean dynamic/shared library or standard library? – Mark B Sep 13 '12 at 20:01
  • C++ without exceptions? _Why_? Because you like the slower and bigger code from checking error codes all the time? Or because you want faster and smaller code from ignoring error codes? No exceptions also makes most C++ libraries useless to you. (Though the [EASTL](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html) is designed for no-exceptions) – Mooing Duck Sep 13 '12 at 20:13
  • Might help: http://stackoverflow.com/questions/553103/can-i-disable-exceptions-in-stl . And this: http://playtechs.blogspot.com/2007/07/64-kb-demos-replacing-crt-heap.html Just search for "c++ stl without exceptions" "std::_Raise_handler" – PiotrNycz Sep 13 '12 at 20:17
  • @Wug and Mooing Duck: One possibility is an embedded system. Imagine what would happen if the flight control system of an airliner flying at 30,000 feet threw an exception. They're not allowed. Neither is `malloc`. – David Hammen Sep 13 '12 at 20:28
  • @DavidHammen: well hopefully, you'd catch the exception and handle it gracefully. I honestly can't see any additional safety in homerolling your own standard library and memory allocation. Computers are deterministic. It's possible to write code that's perfect. It gets harder and harder as complexity increases, but that's why programmers put so much effort into modularizing things. All else fails, this is the reason planes have pilots. – Wug Sep 13 '12 at 20:39
  • @DavidHammen: Exceptions don't crash programs. Undefined behavior crashes programs. Error codes are more likely to cause undefined behavior than exceptions. Ergo, if I'm want a flight control system, I wouldn't _dare_ trust one that didn't use exceptions. – Mooing Duck Sep 13 '12 at 20:42
  • @DavidHammen - re: "imagine what would happen if the flight control system ... threw an exception": imagine what would happen if it **didn't**! – Pete Becker Sep 13 '12 at 20:43
  • @Wug : we are running in a pre-boot environment (BIOS). – Short Sep 13 '12 at 20:45
  • @PeteBecker and Mooing Duck, Those systems do detect errors; FDIR is a big and very complex part of the code. Exceptions? No. There are no exceptions to the no exceptions rule for flight software. I don't want to go into how truly bad of an idea it is to use exceptions in response to failures. – David Hammen Sep 13 '12 at 21:35
  • @Short: I'm not sure how to accomplish this on microsoft's compiler, but I'm pretty sure you can do it with gcc with the `-static` option. As for the memory allocation issue, you might be more out of luck. You might be able to make it work with an allocator class, and store the contents on the stack. – Wug Sep 13 '12 at 21:40
  • That the Joint Strike Fighter software (Stroustrop helped write the standards) excludes exceptions is not so surprising. Perhaps more surprising: LLVM does the same. They have a "no exceptions" rule, and there are no exceptions. – David Hammen Sep 14 '12 at 15:57

3 Answers3

3

It depends upon what you need, but probably, no.

All of the containers in the C++ Standard Library require memory allocation, which is included in a runtime library. So, you can't have:

std::vector<>
std::set<>
std::map<>
std::list<>

You might be able to use

std::array<>
std::tuple<>

But you might not, too.

All of the streams require memory allocation, and most of the streams require file i/o system calls, so they are out, too.

Since you can't use containers, most of the functionality of iterators are no longer useful.

The good news is most Boost support is header-only, so you don't need to link with the Boost library. For Boost, just try #includeing the appropriate header. If it compiles and links, you win.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • But `Boost` probably also relies on exceptions, just like the STL – Mooing Duck Sep 13 '12 at 20:11
  • So, even more probably, "no." – Robᵩ Sep 13 '12 at 20:37
  • When we tried this approach, our first include brought in exceptions. We included boost::container::string and instantiated a default string. We got warnings about exceptions being used immediately, and after investigation found boost::container::string uses "throw e", we replaced that with our user-defined function throw_exception, but ran into issues as boost uses STL. Since we used STL, we defined #define _HAS_EXCEPTIONS 0 to disabled STL exceptions. Unfortunately BOOST includes which has classes which derive from std::exception. That was a dead-end for us. – Short Sep 13 '12 at 20:58
  • But does OP mean "shared libraries" that are loaded at runtime. One assumes that you need the standard runtime to run any C or C++ application without that it would be very hard to do anything. – Martin York Sep 13 '12 at 22:40
2

First off, a point of clarification. You don't need Boost to use the standard template library. The STL was incorporated into C++ a long, long time ago as a part of the C++ standard library. You already have it as a part of visual studio 2010.

You need to clarify what you mean by not being able to "link any runtime libraries". Are you not even allowed to use the standard C and C++ runtime libraries? That means no I/O, no math functions, no new, no delete, no nothin'. If you are allowed to use the standard libraries, then you already have the STL.

There's a catch here, however. That catch is that the C++ standard library throws exceptions. Exceptions are the preferred mechanism by which the standard library functions report errors. Those functions throw std::bad_alloc if they can't allocate memory as required, they throw std::out_of_range when you use the accessors that check whether things are in range, etc. There are lots of invocations of throw in those standard headers.

Does your "no throwing exceptions" apply only to you, or does it apply to the standard library as well? If it applies to the library as well as you programmers, you can write off C++ containers, C++ I/O, C++ strings, and almost all of Boost.

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • the "no throwing exceptions" applies to my team, and libraries that we link in. Currently we are not linking any runtime libraries, including the C/C++ runtime libraries. We run in a pre-boot environment (BIOS), where we don't have access to anything provided by the OS (C runtime libraries use OS hooks) – Short Sep 13 '12 at 20:39
1

In order to eliminate exceptions from the standard library you need a library build that doesn't use exceptions. Setting those macros is part of building and using such a library. I don't think Microsoft ships a no-exception version of their library, but if they do, you need to link to it instead of the normal one. If not, you need to look into licensing the library from Dinkumware, the company that provides it to Microsoft. www.dinkumware.com.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165