Thread: global variable access, from c to c++

  1. #16
    Banned
    Join Date
    Nov 2007
    Posts
    678
    CornedBee, you are probably right about OS specific code
    Mats, I also tried to avoid globals and tried using functions that would set/get the variables.

    Now as provided in the links I can do this on Windows but don't know how to do this on Linux & Mac.

  2. #17
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Well on Linux i did not have to use any kind of trickery to access the functions from a .so file.

    I just made a .c file with two functions in it, into a Linux .so file.
    Then in .cpp i used those functions with this guard around prototypes:
    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    char* toUpper(char* str);
    
    #ifdef __cplusplus
    }
    #endif

    Now, the good news is for my small test, my .so files and functions within are easily used by the .cpp program i made.

    But the bad news is, that it does not work with my project, which also have a similar situation (although already mentioned in my posts above) which i summraize here again:
    The .c files ends up in a .so/.dll or anything OS specific (similiar for a dynamic library). And the program contains all .cpp files. Now the program uses this .dll/.so file (links with it) and needs some functions from it. On Windows there is this dllexport trickery involved. But on Linux nothing needed to be done and i was able to create a .so and access the functions just fine. Is there something more to it (involving .so files) on Linux, which i may be missing?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    From my understanding, .so files exports everything by default, which means you have to do nothing to access everything in your file.
    Windows dlls are different. They export nothing by default. You have to use either __declspec(export) or a definition file (.def) to export your variables/functions to use them elsewhere.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. global struct variable best use
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 06-05-2009, 05:08 AM
  3. controlling modifying global variable
    By sunny_master_07 in forum C Programming
    Replies: 9
    Last Post: 04-11-2008, 04:30 AM
  4. Replies: 2
    Last Post: 09-10-2002, 09:03 PM
  5. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM