0

I have two cpp files and a header file, in one visual studio 2010 c++ project, and I would like to link a ustring variable that's defined in one of the cpp files to the other through the use of extern.

This is the code I have now

\\Tmp.h 
\\Declare tmp_user and tmp_pass
extern ustring tmp_user;
extern ustring tmp_pass;



\\TmpOne.cpp
\\define tmp_user and tmp_pass
#include "Tmp.h"
ustring tmp_user = username;
ustring tmp_pass = password;



\\TmpTwo.cpp
\\use the defined variables
#include "Tmp.h"
login(tmp_user, tmp_pass)
{
    \\some function
}

When I execute this I get a linker error in my TmpTwo.cpp file.

error LNK2001: unresolved external symbol "class ustring tmp_user" (?tmp_user@@3Vustring@@A)

error LNK2001: unresolved external symbol "class ustring tmp_pass" (?tmp_pass@@3Vustring@@A)

If I define tmp_user and tmp_pass in my TmpTwo.cpp file I just get two separate empty files.

What am I doing wrong here?

Adilicious
  • 1,643
  • 4
  • 18
  • 22
  • 1
    Are you linking both `TmpOne.obj` and `TmpTwo.obj`? – Jonathan Leffler Apr 08 '14 at 13:28
  • They are both in the same project but I'm not sure how I would link them? Don't they link through the Tmp.h file? – Adilicious Apr 08 '14 at 13:35
  • Do you place "#include Tmp.h" in them? – user99999991 Apr 08 '14 at 13:37
  • Ah sorry I forgot to add that I'll edit my original question as well but yes I have #include "Tmp.h" in both of these files. – Adilicious Apr 08 '14 at 13:39
  • On a Unix machine from the command line (or makefile), you'd run `g++ -o program TmpOne.cpp TmpTwo.cpp` (or `g++ -o program TmpOne.o TmpTwo.o` if you compile the source to object first). You're on Windows, so your compiler name is different, but you need something similar — perhaps: `cl /o program.exe TmpOne.cpp TmpTwo.cpp` etc. (names and options could be wrong). If you're using an IDE, then you need to tell the IDE to use both object files (source files) to link the program. – Jonathan Leffler Apr 08 '14 at 13:39
  • @JonathanLeffler I'm using Visual Studio 2010 and both of these cpp files and the h file are in one larger project which when compiled creates one executable file. Doesn't this do what you are suggesting? – Adilicious Apr 08 '14 at 13:42
  • Dunno — I've never used VS 2010 (or any earlier version), so I can't help you. – Jonathan Leffler Apr 08 '14 at 13:42
  • From what I've seen VS 2010 does this automatically when the cpp files are in the same project, only if the cpp or h files are in another project do you need to link them in that way. Which brings me back to my original problem. Thanks for your help. – Adilicious Apr 08 '14 at 13:54

1 Answers1

0

There are a few problems here. First, this can't really be the code you're using. Comments start with // not \\ for example. But assuming that it's otherwise close the problem is that you've declared that username and password exist, but you haven't actually instantiated them anywhere.

One of your cpp files needs to include something like this:

ustring username;
ustring password;
Edward
  • 6,964
  • 2
  • 29
  • 55
  • Yeah this isn't my actual code its just to show the issue I'm having. If I remove the extern variables everything builds fine. The username and password are also defined variables I just didn't want to clutter the post with too much information. – Adilicious Apr 08 '14 at 14:20