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?

  • Did your try the things described here? https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Yunnosch Jan 09 '18 at 07:49
  • What with name mangling ? Your file MyMathLib is cpp. Prefix/suffix were added to name of your functions. Read about https://en.wikipedia.org/wiki/Name_mangling and `extern "C"`. – rafix07 Jan 09 '18 at 08:06

2 Answers2

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?

mahesh
  • 11
  • 1
0

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

0___________
  • 60,014
  • 4
  • 34
  • 74
  • 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. – user3717271 Jan 09 '18 at 07:58
  • 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 – 0___________ Jan 09 '18 at 08:02