0

I am following a tutorial here for creating a static library and using it for another project. So I want to create a .lib file and use it for another project.

Static library project:

MyMathLib.h

#define PI 3.1415;
double PowerOf2(double UserNumber);
double PowerOf3(double UserNumber);
double CircleArea(double UserRadius);
double CircleCircum(double UserRadius);

MyMathLib.cpp

#include "stdafx.h"
#include "MyMathLib.h"

double PowerOf2(double UserNumber) { return UserNumber * UserNumber; }
double PowerOf3(double UserNumber) { return UserNumber * UserNumber * UserNumber; }
double CircleArea(double UserRadius) { return UserRadius * UserRadius * PI; }
double CircleCircum(double UserRadius) { return 2 * UserRadius * PI; }

For the second project, I have done the following:

  • Add the MyMathLib vc project

  • Common Properties -> References -> Add New Reference

  • C/C++ -> General -> Additional Include Directories.

This is the C file that tries to call the library:

MyApps1.c

#include <stdio.h>
#include "MyMathLib.h"

int main()
{
    double p2 = 10.0;
    double radius = 4.0;

    printf("The number %.2f to the power of 2 is %.2f. \n", p2, PowerOf2(p2));
    printf("A circle with a radius of %.2f, the area is %.2f. \n", radius, CircleArea(radius));

    return 0;
}

The error I am getting is:

1>------ Build started: Project: MyApps1, Configuration: Debug Win32 ------
1>MyApps1.obj : error LNK2019: unresolved external symbol _PowerOf2 referenced in function _main
1>MyApps1.obj : error LNK2019: unresolved external symbol _CircleArea referenced in function _main
1>c:\users\bandika\documents\visual studio 2013\Projects\MyApps1\Debug\MyApps1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

So there is a linking error somewhere. I have tried going to MyApps1 Properties -> Linker -> Input -> Additional Dependencies but I don't think I can add the .lib file for MyMathLib. Any idea what I'm missing?

2

2 Answers 2

1

Its related to linking of your static lib with the second project.

I don't see any problem in adding your generated static library name in "Configuration Properties -> Linker -> Input -> Additional Dependencies". It should solve the linking problem.

Are you facing any other problem after using this option?

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

Comments

0

You do not have the second file added to the project in the VS.

2 Comments

That would make sense that it cannot find the implementation so the .c must not have been linked. But I believe when I did "Additional Include Directories, I added the directory where both the .h and the .c file were at. I don't think I should be adding files manually too.
Include does not matter in linking. In VS it is a very usual mistake. You need to add the .c file to the project, otherwise the the makefile generator will not know anything about it

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.