Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Shared library question
New Posts  All Forums:Forum Nav:

Shared library question

post #1 of 4
Thread Starter 
I guess this applies to Windows under certain circumstances as well, but this is mainly Linux-related, programming nonetheless.

But this thought just crossed my mind... let's say I've built this wonderful C library. We have our header, wonderful.h:
Code:
typedef struct
{
    short x, y;
} Wonder;

short get_x(Wonder *w);
And of course, wonder.c where get_x is implemented:
Code:
#include "wonder.h"
short get_x(Wonder *w)
{
    return w->x;
}
Now, everyone falls in love with this library, and soon enough it's available on just about every linux distro and most maintainers, of course, compile using a compiler that assumes short means 16-bit integer.

However, someone makes an application that depends on this library, but the compiler thinks short means 32-bit integer for some silly reason. So, the compiler generates code thinking x is 32-bit, not 16-bit, but the library is dynamically linked so this compiler has no effect on the actual width of x and of course it's really only 16-bit... how does this work? Since the widths of data types vary across compilers and even different versions/settings of any one compiler, how can people be sure that their applications won't suddenly nuke themselves, especially in the case of proprietary software where the developers can't know how the libraries on the users' machines are compiler?Is there something about the way shared libraries work that I'm missing?
post #2 of 4
Quote:
Originally Posted by bomfunk View Post

However, someone makes an application that depends on this library, but the compiler thinks short means 32-bit integer for some silly reason. So, the compiler generates code thinking x is 32-bit, not 16-bit, but the library is dynamically linked so this compiler has no effect on the actual width of x and of course it's really only 16-bit... how does this work? Since the widths of data types vary across compilers and even different versions/settings of any one compiler, how can people be sure that their applications won't suddenly nuke themselves, especially in the case of proprietary software where the developers can't know how the libraries on the users' machines are compiler?Is there something about the way shared libraries work that I'm missing?

i think developers can avoid those problems by using compilers that adhere to C++ standards. AFAIK, any compiler worth using would never mistake a short for a 32-bit integer, due to standardization.

According to GCC's website, incompatible libraries would be detected at link time.
Quote:
G++ does not do name mangling in the same way as other C++ compilers. This means that object files compiled with one compiler cannot be used with another.

This effect is intentional, to protect you from more subtle problems. Compilers differ as to many internal details of C++ implementation, including: how class instances are laid out, how multiple inheritance is implemented, and how virtual function calls are handled. If the name encoding were made the same, your programs would link against libraries provided from other compilers--but the programs would then crash when run. Incompatible libraries are then detected at link time, rather than at run time.
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 4
Thread Starter 
Quote:
AFAIK, any compiler worth using would never mistake a short for a 32-bit integer, due to standardization.
But is there an actual standard for this? I know that C (and C++?) guarantee that char<=short<=int<=long as far as the size goes, but I don't know of any standard that specifies the absolute widths, so AFAIK it would be perfectly legal for a compiler to make shorts 32-bit.
Quote:
According to GCC's website, incompatible libraries would be detected at link time.
So, does this mean that if a compiler doesn't follow the same conventions as GCC, a binary compiled with it wouldn't run on a system that's been compiled with GCC?
post #4 of 4
Quote:
Originally Posted by bomfunk View Post

But is there an actual standard for this? I know that C (and C++?) guarantee that char<=short<=int<=long as far as the size goes, but I don't know of any standard that specifies the absolute widths, so AFAIK it would be perfectly legal for a compiler to make shorts 32-bit.
i think we are both partially correct. the C++ standard guarantees that short is at least 16 bits in size. and long is at least 32 bits in size. But this also allows all types to be up to 64-bit. http://en.wikipedia.org/wiki/C_data_types#Basic_types

But, the C++ standard also allows for 'de facto' standards based on the operating system. These 'standards' will define absolute widths based on the operating system application interface.
Quote:
For 64-bit systems, the primary Unix 'de facto' standard is LP64 - long and pointer are 64-bit (but int is 32-bit). The Windows 64-bit standard is LLP64 - long long and pointer are 64-bit (but long and int are both 32-bit).
http://stackoverflow.com/questions/589575/size-of-int-long-etc

if you look at the following link, you will see that LP64, ILP64, LLP64, ILP32, and LP32 all define a short as 16-bits. So while the C++ standard allows a short to be 32-bits, any compiler using one of these standards would not allow a short to be anything other than 16-bits.
http://www.unix.org/version2/whatsnew/lp64_wp.html
Quote:
Originally Posted by bomfunk View Post

So, does this mean that if a compiler doesn't follow the same conventions as GCC, a binary compiled with it wouldn't run on a system that's been compiled with GCC?
yes, i believe that's what it means.
Quote:
This means that object files compiled with one compiler cannot be used with another.

Edited by travesty - 4/5/12 at 4:02pm
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Shared library question