Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Linking to Libraries with Borland C++

Credits go to _Dante for helping out!
This thread is locked; no one can reply to it. rss feed Print
Linking to Libraries with Borland C++
Myrdos
Member #1,772
December 2001

I've been trying to get Borland C++ to link to a custom library, but it invariably results in an exe that gives me an error when I try to run it. Here's my makefile:

all:
  bcc32 -c -wall -tWC TestLib.cpp
  tlib libdtest +TestLib.obj 
  bcc32 -c -wall -tWC testmain.cpp
  ilink32 -ap -Tpe testmain.obj libdtest.LIB

nolib:
  bcc32 -wall mainnolib.cpp

clean:
  -del *.obj
  -del *.LIB
  -del *.tds
  -del *.il?
  -del *.exe
  -del *.map
Windows said:

testmain.exe is not a valid Win32 application.

If I use 'make nolib', I can create an exe that runs just fine. The error only crops up when I try to split things up into a library. I've been fighting with this all day, any help is appreciated.

[EDIT]Here's my source:

1///***** testmain.cpp *****
2#include "TestLib.hpp"
3 
4using namespace std;
5 
6int main(void)
7{
8 TestLib theTest;
9
10 theTest.run();
11}//main
12 
13//***** TestLib.hpp *****
14 
15#ifndef DY_TEST_LIB
16#define DY_TEST_LIB
17 
18#include <iostream>
19 
20class TestLib
21{
22 public:
23 //constructor
24 TestLib(void);
25
26 //run the test
27 void run(void);
28
29};//TestLib
30 
31#endif
32 
33///***** TestLib.cpp *****
34 
35#include "TestLib.hpp"
36 
37using namespace std;
38 
39TestLib::TestLib(void)
40{
41};//constructor
42 
43void run(void)
44{
45 int i = 8;
46
47 cout << i * 8 << endl;
48}//run

__________________________________________________

_Dante
Member #7,398
June 2006
avatar

Whoever designed ilink32 should be shot. From the manual:

Quote:

ILINK32 [@respfile][options] startup myobjs, [exe], [mapfile], [libraries], [deffile], [resfile]

You're required to supply a bunch of borland libraries (C0W32.OBJ, CW32.LIB, and IMPORT32.LIB) when using ilink, and you have to use the syntax and sequence they provide there.

If you're just looking to get it linked real quick and don't feel like screwing around with ilink's command line, use bcc32 to link your objects and it will do the right thing:

1all:
2 bcc32 -c -wall -tWC TestLib.cpp
3 tlib libdtest +TestLib.obj
4 bcc32 -c -wall -tWC testmain.cpp
5 bcc32 testmain.obj libdtest.LIB
6 
7nolib:
8 bcc32 -wall mainnolib.cpp
9 
10clean:
11 -del *.obj
12 -del *.LIB
13 -del *.tds
14 -del *.il?
15 -del *.exe
16 -del *.map

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Myrdos
Member #1,772
December 2001

Still no luck! When I try bcc32 testmain.obj libdtest.LIB I get: "Error: Unresolved external 'TestLib::run()'" Rearranging the order doesn't seem to do anything.

When I try ilink32 /ap testmain.obj C0W32.OBJ, testdave.exe,, libdtest.LIB CW32.LIB IMPORT32.LIB I get: "Error: Unresolved external 'WinMain' referenced from C:\BORLAND\BCC55\LIB\C0W32.OBJ" I tried re-arranging the order of the OBJ and LIB files, but get the same error.

When I try it without C0W32.OBJ, it cranks out an exe. But when I try to run it, I get: "testdave.exe has encountered a problem and needs to close. We are sorry for the inconvenience."

__________________________________________________

_Dante
Member #7,398
June 2006
avatar

Quote:

I get: "Error: Unresolved external 'TestLib::run()'" Rearranging the order doesn't seem to do anything.

Oh sorry, I forgot to mention that your code (specifically TestLib.cpp) has a bug in it. You'd have gotten that error with any compiler/linker combination.

1///***** TestLib.cpp *****
2 
3#include "TestLib.hpp"
4 
5using namespace std;
6 
7TestLib::TestLib(void)
8{
9};//constructor
10 
11// XXX: no scope resolution operator!
12// this is _not_ a member function
13void run(void)
14{
15 int i = 8;
16
17 cout << i * 8 << endl;
18}//run

Change void run(void) to void TestLib::run(void) and all will be well.

-----------------------------
Anatidaephobia: The fear that somehow, somewhere, a duck is watching you

Myrdos
Member #1,772
December 2001

DOH!

I can't believe I didn't see that. Thanks again for the help!

__________________________________________________

Go to: