global variable access, from c to c++

This is a discussion on global variable access, from c to c++ within the C++ Programming forums, part of the General Programming Boards category; i have a c file which contains some global variables. and i have one cpp file which needs to access ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    global variable access, from c to c++

    i have a c file which contains some global variables.
    and i have one cpp file which needs to access these global variables.
    it did the following but failed:
    Code:
    /* file.c */
    int value;
    Code:
    // file.cpp
    extern "C" {
    extern int value;
    }
    i got a linker error saying _value is unresolved symbol.

    i am using MSVC .Net 2003, but i need to be able to compile this on gcc as well. so a portable solution is needed here

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    How do you compile these? With one C and one C++ file I'm sure you don't compile into one single binary?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Hi Magos!

    The .c file ends up in a .dll file, while .cpp file is used for building .exe file which then depends on .dll file as well.

    I tried DUMPBIN on the .dll as follows:
    DUMPBIN /SYMBOLS cfile.dll

    but nothing was output!
    Code:
    File Type: DLL
    
      Summary
    
           4A000 .data
            2000 .idata
           30000 .rdata
            F000 .reloc
          154000 .text

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    you have to declare value as dllexport...

    But I personally prefer not to export globals from dll - instead I provide some reasonable API
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thanks Magos! Thanks vart!
    The above solution would be MS only, i need to do the same in gcc also.

    I tried to use 2 functions instead of globals, one function will set the values, other will get the values.

    But again same linker error of unresolved symbols

    vart what do you suggest as a reasonable API here?

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    DLL's are windows-specific anyways
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Yeah Magos, but as you asked:
    "How do you compile these? With one C and one C++ file I'm sure you don't compile into one single binary?"
    So i told you what the files end up as. It may not be in .dll or .so file on Linux, maybe different situation on Mac
    But the c file and cpp file, both are needed for final .exe file. it's just that c file happens to be compiled in a .dll on windows, on linux it will be different.

    but why i am unable to access global variables or functions from my c file?

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    vart what do you suggest as a reasonable API here?
    I do not know what you want to do with this global var...

    One of the ways...

    Provide Init that will dynamically allocate all neded data and return handle to this memory
    Every other funtion in Dll will receive the handle - cast it to the real data type and perform required actions

    End function will release the allocated memory making handle invalide

    In such approach DLL could be used in MT environment without big problems, and also work on several instancies of the data simulteneously...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    vart, the c file contains some low level audio stuff, cpp file is using Qt to make gui over the low level stuff in c file. so using gui controls i want to set some values in the c file. my first thought was making the values global in c file and using them in cpp file. but then i made a set/get pair of functions to do the same. both approaches give me unresolved symbol linker error.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    have you used __declspec(dllexport) declarations as suggested by the links provided?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    No vart, that will work with MSVC only. I need to compile on Windows, MacOS & Linux.
    Isn't there a portable approach?
    I thought that at least in case of functions I will be able to access them from my c file, regardless of whether the c file ends up as a .dll or .so or anything.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    you cannot compile dll on other OSes... so use #ifdef

    Or - do not use dll at all, compile it as static library
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Isn't there a portable approach?
    No. You need OS-specific code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So what is the reason you can't pass the variable from one side to the other?

    Global variables are a nuisance, and it gets worse when you have to pass them across shared library/DLL boundaries.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21