0

In a C++ Win32 program I need to use the same string variable in the modules

main.cpp and client.cpp. Both main.cpp and client.cpp have a #include client.h

where I try to declare the global(extern) string.

Let us call the string "test_string"

How do I declare the string as global so I can use it in both modules?

Hope the question is clear enough.

user1288615
  • 41
  • 1
  • 1
  • 5
  • Not a duplicate, the answer in the link won't work for Windows. You need to export the global variable with `declspec` for this to work in Windows. Edit: Although if it is only for multiple files and not dlls, then extern will work. – AndyG Oct 27 '16 at 11:46
  • the examles in the other questions declare int, that is working OK in my program, but the how do I do it for strings? I can't get it to work – user1288615 Oct 27 '16 at 11:58
  • @user1288615 when you say string do you mean a `std::string` or a c-string? – NathanOliver Oct 27 '16 at 12:04
  • "I can't get it to work" is not enough information. Please provide the full error message and the minimum code to reproduce it. – AndyG Oct 27 '16 at 12:06
  • I mean a std::string – user1288615 Oct 27 '16 at 12:10
  • Then it should work exactly the same. If it does not please provide a [mcve] – NathanOliver Oct 27 '16 at 12:13
  • This is the error meassage I get from the compiler: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int error C2146: syntax error: missing ';' before identifier 'test_string' error C2371: 'test_string': redefinition; different basic types note: see declaration of 'test_string' – user1288615 Oct 27 '16 at 13:07
  • i use extern string test_string; which is the cause of the problem – user1288615 Oct 27 '16 at 13:12
  • but, what do I need to use instead of extern string test_string? – user1288615 Oct 27 '16 at 13:54

0 Answers0