0

I'm facing the error when I'm linking an external library and code with the current project. You will understand the problem by this example.

//foo.h
namespace LMS
{
    class foo
    {
        foo();         //implementation in cpp
        foo(int dummy) //implemented here
        {
            //something
        }
    };
} // namespace LMS
//foo.cpp
#include"foo.h"
namespace LMS
{
    foo::foo()
    {
        //something
    }
} // namespace LMS
//externalProgram.h
#include "foo.h"

LMS::foo *ptr;
ptr = new LMS::foo();  //Linking error: LNK 2019
ptr = new LMS::foo(2); //No error

Now the problem is that the external program doesn't know about the foo.cpp and the implementation of class foo methods in it. I'm currently using VS2019 and using two projects in the same solution. I've tried several ways to correct this but it didn't work out. The current way I'm seeing is to implement all the functions in header files.

EDIT: I've already linked the files!! enter image description here enter image description here

9
  • Hopefully foo.cpp includes foo.h as well. Commented Dec 25, 2020 at 9:30
  • Yes, that was a typo in question. Commented Dec 25, 2020 at 9:32
  • After trying the simpler, rebuild all, I would start checking the actual artifacts from build of LMS to make sure the LMS.a has the right object files in it, and the object files have the right symbols. At this point you're looking for something subtle, because on the surface it should work. Commented Dec 25, 2020 at 9:35
  • 1
    Cannot reproduce the issue, assuming I apply some fixes: 1. Make the constructors public 2. append a semicolon to the class declaration 3. Use a .cpp file instead of a .h file for externalProgram.h (not how this is included otherwise) 4. Move the new statements to a function. Commented Dec 25, 2020 at 9:39
  • I was looking in the output tab and it was compiling all the files without any warnings or error. I was actually able to open lms.lib file, but not working in the external program Commented Dec 25, 2020 at 9:39

1 Answer 1

1

You should be able to mark your externalProgram project as dependent on the foo project. This will link foo.o in with external program.

Using the UI you will select this in
Project > Project Dependencies : Depends on ...

If that is correct, then the problem is more subtle, sometimes just a simple typo. At this point you want to use the command line tools to break apart the library and confirm the object files, and break apart the object files and confirm the symbols within. A ten year old SO posting discusses using lib.exe to example the library file. The dumpbin tool has a /symbols option that might also be handy for looking at the actual code generated.

Sign up to request clarification or add additional context in comments.

1 Comment

Already done. I'll attach the screenshot in a minute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.