0

I'm trying to create a global variable, within my VS2017 solution, to share an integer between (2) CPP files. I thought this was a no brainer, but for the life of me I can't shake this darn LNK1120 error. This should be so easy, please tell where I'm screwing up.

I tried a very simple program, that doesn't work for me (get LNK1120). Is there something specific that needs to be setup in VS2017? I have tried making the cpp files dependent on each other but no dice ...

global.cpp:

int g_x;
int g_y(2);

main.cpp:

#include <iostream>
extern int g_x;
int main()
{
    extern int g_y;
    g_x = 5;
    std::cout << g_y; // should print 2
    return 0;
}

The above example is from this website (https://www.learncpp.com/cpp-tutorial/42-global-variables/)

The code compiles okay, but the Linker keeps throwing the errors below;

Error   LNK2001 unresolved external symbol "int g_x" (?g_x@@3HA)    
Error   LNK2001 unresolved external symbol "int g_y" (?g_y@@3HA)    
Error   LNK1120 2 unresolved externals

This leads me to believe that there is something that needs to be setup in the Linker configuration settings? What am I missing here?

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
99Boboster99
  • 63
  • 1
  • 9
  • 2
    Did `global.cpp` build as part of your executable? – drescherjm Jun 17 '19 at 20:37
  • Also, after you learn global variables please learn the reasons why they should be avoided if possible. – drescherjm Jun 17 '19 at 20:38
  • @drescherjm that is too harsh. Did you ever use `std::cout` in your program? – SergeyA Jun 17 '19 at 20:40
  • Ah, that is the next lesson: https://www.learncpp.com/cpp-tutorial/4-2a-why-global-variables-are-evil/ – drescherjm Jun 17 '19 at 20:45
  • Yes, 'global.cpp' built as part of my executable. It's the Linker that is getting tripped up. What is the next best method to use to share variables? I'd hate to have to write to a file and then read from it in the other cpp program ... what is this 1984 ... :) – 99Boboster99 Jun 17 '19 at 21:19
  • @99Boboster99 Sorry but global.cpp is not built as part of your executable or you wouldn't be getting that error message. – john Jun 17 '19 at 21:25
  • @99Boboster99 Incidentally that tuorial you are following is using incorrect terminolgy, forward declaration means something different from what the tutorial thinks it means. – john Jun 17 '19 at 21:28
  • I read the "global variables" are evil doc and went with using namespaces, works like a charm now! :) thanks you gents!! – 99Boboster99 Jun 17 '19 at 21:38
  • Hey John, you were correct, the file global.cpp was not part of the same executable! Even with the namespace test (which I did with separate cpp files that ARE part of the same executable) .. I just tested with the files not part of the same exe and it fails! What technique is there to share memory between different exe or dll files? Do I need to use GetProcAddress? – 99Boboster99 Jun 17 '19 at 22:21
  • https://www.boost.org/doc/libs/1_70_0/doc/html/interprocess/sharedmemorybetweenprocesses.html – drescherjm Jun 18 '19 at 03:00
  • Any way to do it without a 3rd party addon? – 99Boboster99 Jun 18 '19 at 10:12
  • Several ways. memory mapped files are one. https://learn.microsoft.com/en-us/dotnet/standard/io/memory-mapped-files – drescherjm Jun 18 '19 at 11:15
  • https://stackoverflow.com/questions/8641738/how-to-create-a-share-memory-pool-in-windows – drescherjm Jun 18 '19 at 11:18
  • Thank you for the link drescherjm, any idea why it won't read the memory area from within a "extern "C"" routine? the memory area creates okay in "project1.cpp", but I can't access it from "project2.cpp" that contains a "extern "C"" routine??? – 99Boboster99 Jun 20 '19 at 22:09

0 Answers0