Jump to content

  • Log In with Google      Sign In   
  • Create Account

SiCrane

Member Since 09 Dec 1999
Offline Last Active Private

Posts I've Made

In Topic: C++ interfaces and library decoupling

24 January 2016 - 07:34 PM

Because of this, code compiled with C++ compiler A cannot talk to code compiled with C++ compiler B. And compiler vendor A could change their mind between versions, changing things again, so you can't even use code compiled with different compilers from the same vendor.

This is an over-generalization. Again, COM interfaces are quite frequently implemented as C++ classes and work quite well across compilers on Windows. You can get a pointer to a COM object implemented as a MSVC C++ class and directly invoke a member function on it from BCB compiled C++ program and vice versa. You can't freely inter-operate between compilers (in fact, interface classes whose arguments are only primitive and pointer types is about the only C++ feature that works reliably), but that's different from saying it can't work at all.
 

In comparison, C defines a standard binary layout for its structs, arrays, and the like, as well as things like not mangling function names. Because of this, C code compiled with any compiler, can talk to C code compiled by any other compiler.

Actually, it doesn't. In particular, primitives don't have set sizes (only minimum sizes) and structs may have vendor specific padding between members. It's also entirely possible for one compiler for a given platform to have primitive types with sizes unavailable to some other compilers on the same platform (historically common with 64-bit and 128-bit types). There's also the fun realm of incompatible vendor specific calling conventions (ex: Microsoft C, Turbo C and Watcom C all defined a __fastcall convention but each used a different number of registers). For that matter, some vendors just never bothered implementing broad parts of C99, so any features from that aren't guaranteed to be portable. Usually the only ones that cause problems for library interfaces are flexible array members and the _Bool type.
 

Functions must be defined under C-type calling convention, cannot have any overrides, and cannot be members of structs. Classes cannot be exposed.

Actually, on x86 Windows most cross-compiler code seems to use the Pascal calling convention (arguments passed left to right with callee cleanup) rather than C calling conventions (arguments passed right to left with caller cleanup). In particular, almost every Win32 API function uses the Pascal calling convention. And again, in COM, C++ interface classes are frequently exposed.


In Topic: C++ interfaces and library decoupling

23 January 2016 - 07:51 PM

To be fair, each library still has to know about the other in COM because they have to have access to the interface definition.

Not necessarily. It's a lot easier if the static types of all the interfaces necessary are available, but a library that implements the IDispatch interface can be dynamically interrogated and manipulated. Both libraries need to know about COM automation, but they don't necessarily need to know about each other. For example, this is how scripting support for new components was implemented in Office (up to around 2003, no idea if later versions of Office changed their inner workings). If you added a new charting widget to your worksheet, Excel wouldn't know about new interfaces it added over the base OLE stuff, but you could still manipulate them through VBA because the charting widget would implement the IDispatch interfaces VBA would use to make the actual calls.

For dynamic libraries your options are far more limited, as C++ cannot be used. (Insert arguments about how C++ can be used under special conditions A, B, or C) In most cases you'll be dealing with C APIs and things like function pointers.

You may want to be a lot more clear about what you mean by "C++ cannot be used" as it's pretty obvious that people do write dynamic libraries in C++. Again, many COM DLLs are written in C++. Right now your statement only makes sense if you already know what it means.

In Topic: C++ interfaces and library decoupling

23 January 2016 - 05:21 PM

With proper design firewalls that can work. In particular you need to make sure that the libraries are compiled in a compatible manner (ex: both libraries agree what the size of the various types are) and one binary doesn't mess with the implementation details of the other (ex: no deleting something new'd by the other - note that this means that if your interface contains inline functions, in particular template code which is implicitly inlined, then you may have problems). One example of this at work is COM, which allows binaries built by different compilers (and programming languages) to work together on Windows.


In Topic: Python for 1st language?

08 January 2016 - 10:48 PM

Nothing wrong with Python as a first language, but if your eventual goal is PC programming with C++, you may want to start with C# instead. Difficulty-wise it's roughly equal to Python, and it's closer to C++. In terms of game programming C# is probably easier than Python with better libraries and frameworks available.


In Topic: Should I Take AP Biology?

07 January 2016 - 10:53 PM

A lot of universities give benefits for students with more credit hours such as being able to register earlier or being able to more easily qualify for better parking permits. Usually one AP test by itself wouldn't make much of a difference for this purpose, but you're already interested in two more. So if you think you can get a four or five on the AP exam, I'd recommend taking the course even if you don't think it would apply towards your major.


PARTNERS