🔒 Locked
Cross project variable access
Advertisement
Original Post
I think the solution has something to do with externs, but for some reason or another that has always been one of my weaknesses. I have a variable defined in a .cpp file in one project (ProjectA), and I need to access that same variable in a .cpp file in a different project(ProjectB). I would normally have defined the variable in a .h file, but this is code that was not written by me, and there are multiple variables of that same name throughout ProjectA. Any advice would be appreciated, and thanks in advance. edit: Forgot to mention the basics. I am working with C++, in Visual Studio, on windows.
Is this variable in two different EXEs? One way would be to wrap these global variables into a class or structure and pass the structure around via some sort of pattern (Visitor or Proxy). If they are in two different memory spaces, you can always package one project into a DLL and export its functions/members.
This is a massive code base, I wanted to try and alter as little as possible. I was thinking about wrapping the variable into a class, which is probably my best solution at this point. And it is between 2 EXEs.
Quote:
Original post by Ultraseamus
I think the solution has something to do with externs, but for some reason or another that has always been one of my weaknesses.
I have a variable defined in a .cpp file in one project (ProjectA), and I need to access that same variable in a .cpp file in a different project(ProjectB). I would normally have defined the variable in a .h file, but this is code that was not written by me, and there are multiple variables of that same name throughout ProjectA. Any advice would be appreciated, and thanks in advance.
edit: Forgot to mention the basics. I am working with C++, in Visual Studio, on windows.
1) Is the CPP file actually part of both projects?
2) Have you read this?
OK, I decided the easiest solution would be to make a static function in a class from that project that I had access to, and from there I return the variable I need. Problem now is that ProjectB thinks that the function from ProjectA is unresolved, because ProjectB can not see that the function is defined in a .cpp file in ProjectA. Once again, I think this is an extern situation, but it will not allow me to extern the function because it is a member function. And I would define the function in the .h, but the variable is in the .cpp. Before I tried this solution I tired just declaring the variable in the .h, but that caused numerous issues with other files trying to access it.
Am I going down a completely wrong path, or will this solution work? One possible solution would be to create a whole new variable in the class, and as early as I can set it to equal the other variable (a pointer by the way). But I hate writing sloppy code, and this just does not seem like the best solution considering the possibility of the original variable being altered in the future.
Sorry if this is hard to follow, I confused myself multiple times when writing it.
Am I going down a completely wrong path, or will this solution work? One possible solution would be to create a whole new variable in the class, and as early as I can set it to equal the other variable (a pointer by the way). But I hate writing sloppy code, and this just does not seem like the best solution considering the possibility of the original variable being altered in the future.
Sorry if this is hard to follow, I confused myself multiple times when writing it.
SiCrane: The thought of that scares me. I am still new to this code base, so I am going to hope I was wrong about the 2 projects producing 2 EXEs. If that is the case then extern could work to solve my problem?
OK, ProjectB produces an EXE, ProjectA produces a dll. Sorry for not having that information sooner.
The one which results in a dll has the variable I need.
That sounds like exactly what I need, but I tired it and so far it is not working. In the .cpp file in the .dll project I have:
__declspec(dllexport) myClass *myPointer = NULL;
and in the .cpp in the EXE project I have:
__declspec(dllimport) myClass *myPointer;
And I get:
error LNK2001: unresolved external symbol.
Reading the MSDN entry this looked like this would be the correct usage, what am I doing wrong?
__declspec(dllexport) myClass *myPointer = NULL;
and in the .cpp in the EXE project I have:
__declspec(dllimport) myClass *myPointer;
And I get:
error LNK2001: unresolved external symbol.
Reading the MSDN entry this looked like this would be the correct usage, what am I doing wrong?
No, and it is taking my longer then expected to remember how. This is an aspect of programming that I always kind of ignored in the past.
I figured it out, that worked perfectly. Thank all of you for your help. I can now access that variable, and my only other problem is an unresolved external error when I try to access a member function (from the class to which the variable is pointing). Should be close to the same problem as before, and I will try the same solution.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement