rsood
asked on
How to resolve externals ?
Here is the situation:
- point.h define a class Point and contains prototype definitions of all its methods.
- point.cpp defines all its methods.
- myProg2.cpp is a program that instantiates class Point.
I have created a DLL, point.dll as follows:
cl -GX -LD point.cpp -Fepoint.dll
I tried the following to build the binary but got link errors:
G:\C++\DLL>cl -GX myProg2.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
myProg2.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:myProg2.exe
myProg2.obj
myProg2.obj : error LNK2001: unresolved external symbol "public: __thiscall point::point(double,double )" (??0point@@QAE@NN@Z)
myProg2.exe : fatal error LNK1120: 1 unresolved externals
Question: How do I resolve the 'unresolved externals' ???
----------------- start of point.h ------------------------
// point.h
class point {
private:
double x;
double y;
public:
point();
point (double, double);
void printPoint();
double getX();
double getY();
void setX(double);
void setY(double);
};
----------------- end of point.h ------------------------
----------------- start of point.cpp ------------------------
// point.cpp
#include <iostream>
#include "point.h"
using namespace std;
// default constructor
point::point() {
x = 0.0;
y = 0.0;
}
// constructor with params
point::point(double pointX, double pointY) {
x = pointX;
y = pointY;
}
double point::getX() {
return x;
}
double point::getY() {
return y;
}
void point::setX(double pX) {
x = pX;
}
void point::setY(double pY) {
y = pY;
}
void point::printPoint() {
cout << "Class point: x=" << x << " y=" << y << " ";
}
----------------- end of point.cpp ------------------------
----------------- start of myProg2.cpp ------------------------
// Example2: file myProg2.cpp
#include <iostream.h>
#include "point.h"
int main()
{
cout << "Hello, world!\n";
// create instances of two point, p1 & p2
point p1(4.0, 4.0), p2(5.0, 5.0);
return 0;
}
----------------- end of myProg2.cpp ------------------------
- point.h define a class Point and contains prototype definitions of all its methods.
- point.cpp defines all its methods.
- myProg2.cpp is a program that instantiates class Point.
I have created a DLL, point.dll as follows:
cl -GX -LD point.cpp -Fepoint.dll
I tried the following to build the binary but got link errors:
G:\C++\DLL>cl -GX myProg2.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
myProg2.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:myProg2.exe
myProg2.obj
myProg2.obj : error LNK2001: unresolved external symbol "public: __thiscall point::point(double,double
myProg2.exe : fatal error LNK1120: 1 unresolved externals
Question: How do I resolve the 'unresolved externals' ???
----------------- start of point.h ------------------------
// point.h
class point {
private:
double x;
double y;
public:
point();
point (double, double);
void printPoint();
double getX();
double getY();
void setX(double);
void setY(double);
};
----------------- end of point.h ------------------------
----------------- start of point.cpp ------------------------
// point.cpp
#include <iostream>
#include "point.h"
using namespace std;
// default constructor
point::point() {
x = 0.0;
y = 0.0;
}
// constructor with params
point::point(double pointX, double pointY) {
x = pointX;
y = pointY;
}
double point::getX() {
return x;
}
double point::getY() {
return y;
}
void point::setX(double pX) {
x = pX;
}
void point::setY(double pY) {
y = pY;
}
void point::printPoint() {
cout << "Class point: x=" << x << " y=" << y << " ";
}
----------------- end of point.cpp ------------------------
----------------- start of myProg2.cpp ------------------------
// Example2: file myProg2.cpp
#include <iostream.h>
#include "point.h"
int main()
{
cout << "Hello, world!\n";
// create instances of two point, p1 & p2
point p1(4.0, 4.0), p2(5.0, 5.0);
return 0;
}
----------------- end of myProg2.cpp ------------------------
ASKER
I tried as you sugested but got the following error:
G:\C++\DLL>cl -GX myProg2.cpp /link point.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
myProg2.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:myProg2.exe
point.lib
myProg2.obj
LINK : fatal error LNK1181: cannot open input file "point.lib"
I do not see point.lib in my directory. Is it supposed to be created when point.dll is created ?
G:\C++\DLL>cl -GX myProg2.cpp /link point.lib
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
myProg2.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:myProg2.exe
point.lib
myProg2.obj
LINK : fatal error LNK1181: cannot open input file "point.lib"
I do not see point.lib in my directory. Is it supposed to be created when point.dll is created ?
>>Is it supposed to be created when point.dll is created
Yes, it should be in the same directory as 'point.dll'. You can also specify an absolute path to the .lib file.
BTW, I just noticed a small problem with your class - it should be
// point.h
#ifdef POINT_DLL
#define __DYNLINK __declspec ( dllexport)
#else
#define __DYNLINK __declspec ( dllimport)
class __DYNLINK point { // either export or import, depending on what we are compiling
private:
double x;
double y;
public:
point();
point (double, double);
void printPoint();
double getX();
double getY();
void setX(double);
void setY(double);
};
// point.cpp
#include <iostream>
#define POINT_DLL
#include "point.h"
If you don't export your class, you will not get a lib file.
Yes, it should be in the same directory as 'point.dll'. You can also specify an absolute path to the .lib file.
BTW, I just noticed a small problem with your class - it should be
// point.h
#ifdef POINT_DLL
#define __DYNLINK __declspec ( dllexport)
#else
#define __DYNLINK __declspec ( dllimport)
class __DYNLINK point { // either export or import, depending on what we are compiling
private:
double x;
double y;
public:
point();
point (double, double);
void printPoint();
double getX();
double getY();
void setX(double);
void setY(double);
};
// point.cpp
#include <iostream>
#define POINT_DLL
#include "point.h"
If you don't export your class, you will not get a lib file.
ASKER
Thanks for your help but I am absolute newbie and need a little more help to get this example going. (I have raised the points also.)
After modifying point.h and point.cpp, I am getting the following compilation errors:
G:\C++\DLL>cl -nologo -GX -LD point.cpp -Fepoint.dll
point.cpp
point.h(21) : fatal error C1004: unexpected end of file found
-------- start of point.h ----------
// point.h
#ifdef POINT_DLL
#define __DYNLINK __declspec ( dllexport)
#else
#define __DYNLINK __declspec ( dllimport)
// either export or import, depending on what we are compiling
class __DYNLINK point {
private:
double x;
double y;
public:
point();
point (double, double);
void printPoint();
double getX();
double getY();
void setX(double);
void setY(double);
};
-------- end of point.h ----------
-------- start of point.cpp ----------
// point.cpp
#include <iostream>
#define POINT_DLL
#include "point.h"
using namespace std;
// default constructor
point::point() {
x = 0.0;
y = 0.0;
}
// constructor with params
point::point(double pointX, double pointY) {
x = pointX;
y = pointY;
}
double point::getX() {
return x;
}
double point::getY() {
return y;
}
void point::setX(double pX) {
x = pX;
}
void point::setY(double pY) {
y = pY;
}
void point::printPoint() {
cout << "Class point: x=" << x << " y=" << y << " ";
}
-------- end of point.cpp ----------
After modifying point.h and point.cpp, I am getting the following compilation errors:
G:\C++\DLL>cl -nologo -GX -LD point.cpp -Fepoint.dll
point.cpp
point.h(21) : fatal error C1004: unexpected end of file found
-------- start of point.h ----------
// point.h
#ifdef POINT_DLL
#define __DYNLINK __declspec ( dllexport)
#else
#define __DYNLINK __declspec ( dllimport)
// either export or import, depending on what we are compiling
class __DYNLINK point {
private:
double x;
double y;
public:
point();
point (double, double);
void printPoint();
double getX();
double getY();
void setX(double);
void setY(double);
};
-------- end of point.h ----------
-------- start of point.cpp ----------
// point.cpp
#include <iostream>
#define POINT_DLL
#include "point.h"
using namespace std;
// default constructor
point::point() {
x = 0.0;
y = 0.0;
}
// constructor with params
point::point(double pointX, double pointY) {
x = pointX;
y = pointY;
}
double point::getX() {
return x;
}
double point::getY() {
return y;
}
void point::setX(double pX) {
x = pX;
}
void point::setY(double pY) {
y = pY;
}
void point::printPoint() {
cout << "Class point: x=" << x << " y=" << y << " ";
}
-------- end of point.cpp ----------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are a great help ! Thanks again.
cl -GX myProg2.cpp /link point.lib
or by placing a directive in 'myProg2.cpp':
#pragma comment ( lib, "point.lib")