I have a question about using Visual Studio to compile c++ codes.
I created 3 projects: A, B and C. A and B were created as dynamic library, while C was created
as static library. There are 3 classes, with one defined in each project.
ClassA is defined in the project A:
ClassA
{
public
testA();
};
ClassB is defined in the project B.
ClassB: public ClassC
{
public:
testB();
};
ClassC is defined in the project C.
ClassC
{
public:
testC();
};
In Visual Studio, A has a dependency on B, and B has a dependency on C. But A doesn't have dependency on C.
Also the implementation of ClassA::testA is:
ClassA::testA ()
{
ClassB b;
b.testC();
}
When I build project A, I got a link error which complained external symbol of testC cann't be found.
How can I solve this issue without creating dependency between A and C?
J:\Temp\27>make.bat
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
27c.cpp
Microsoft (R) Library Manager Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
27b.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:27b.exe
/dll
/out:27b.dll
27c.lib
27b.obj
Creating library 27b.lib and object 27b.exp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
27a.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:27a.exe
/dll
/out:27a.dll
27b.lib
27a.obj
Creating library 27a.lib and object 27a.exp
27a.obj : error LNK2019: unresolved external symbol "public: void __thiscall ClassC::testC(void)" (?testC@ClassC@@QAEXXZ) referenced in function "public: void __thiscall ClassA::testA(void)" (?testA@ClassA@@QAEXXZ)
27a.dll : fatal error LNK1120: 1 unresolved externals
Code:
J:\Temp\27>make.bat b-exports-c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
27c.cpp
Microsoft (R) Library Manager Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
27b.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:27b.exe
/dll
/out:27b.dll
27c.lib
27b.obj
Creating library 27b.lib and object 27b.exp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
27a.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:27a.exe
/dll
/out:27a.dll
27b.lib
27a.obj
Creating library 27a.lib and object 27a.exp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
27x.cpp
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:27x.exe
27x.obj
27a.lib
Last edited by Igor Vartanov; November 27th, 2013 at 02:31 AM.
This statement is incorrect.
A is dependent on C because it uses ClassB which publicly derives from C and you are thus making direct use of an inherited class member of C. This requires you to have access to stuff in C.
This is simply because of how inheritance works from a technical POV. It would seem you are assuming that deriving from B from C that the compiler "inserts" copies of the C member into B so they can be called as ClassB::testC while in reality ClassB::testC doesn't actually exist but is an alias the compiler allows to ClassC::testC.
You can either make A dependant on C
Or you can explicitely provide the
ClassB::testC() { parentclass.testC(); }
member.
note that "parentclass" isn't a valid syntax, VC++ allows the nonstandard __super instead. Some other compilers have other means. the portable solution is "knowing" what your parent is and writing it as ClassC::testC();
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.