• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

How do I link a 3rd party library/api into my c++ programs using bcc32?

Hi everyone!

I wonder if anyone can help me out.  I have recently started programming with C++, and I am trying to create a portable application using the wxWindows API [http://www.wxwindows.org/], using the free download of the Borland C++ compiler (bcc32) under WindowsXP.  I have managed to follow the instructions in the wxWindows documentation and have installed the library into the default 'C:\wxWindows_2.4.0\' folder.  bcc32 is installed into the default 'C:\Borland\ folder', and my source code is in 'C:\src\'.  I have also compiled the library (producing a .lib file in 'C:\wxWindows_2.4.0\Lib\'), and have successfully compiled and run a sample program supplied with wxWindows.

My problem is that when I try to compile a Hello World program, as described by the wxWindows tutorial, the compiler complains that it cannot find the 'wx/wx.h' header file that I am required to include in my source.  I tried to solve the problem by copying the contents of 'C:\wxWindows_2.4.0\Include\wx\' to 'C:\Borland\Include\wx', but the compiler then complained that it could not find the 'setup.h' header file which I think is required by the 'wx.h' header.  I located 'setup.h' in another folder and copied this to 'C:\Borland\Include\wx' and tried to compile again, but this time the compiler complained that it could not find 'HelloWorldApp.h' - a header file written by me and placed into the same folder as the .cpp source file ('C:\src\').  I should mention that I am running the compiler from the 'C:\src\' folder when trying to compile my program, but I used the supplied makefile from inside the 'C:\wxWindows_2.4.0\Samples\' folder when I successfully built the sample program.

I think my problem is that I do not know how to set up the wxWindow library so that the compiler can find everything that it needs.  Everything I have done up until now has just used the standard-library.  I have triple checked my source code and I am confident that this is not causing any problems.  Also I am trying to do everything from the command line, i.e. without an IDE.  What is confusing me is that the sample apps provided with the library compile just fine, but my own app won't.  Do I need to write a makefile for my app?

If anyone can help point me in the right direction I would be very grateful.  Thanks for your time, and sorry for the long post :-)

Tim Grocott

P.S. I am aiming to write a DNA/protein sequence analysis application that will port between PC and Mac.  If I ever get it working it will save myself and others a lot of time at work!  It will be open source, free for academic/educational use, and probably licensed under the GPL.
0
TimGrocott
Asked:
TimGrocott
  • 6
  • 6
1 Solution
 
DarthNemesisCommented:
Not sure if this is the cause of the problem, but when you include a file in the same directory as your source (as opposed to the include directory) you have to use "file.h" instead of <file.h>.
0
 
TimGrocottAuthor Commented:
Hi DarthNemesis!

Thanks for posting.  I wasn't aware there was a difference between <file.h> and "file.h".  I changed the .cpp file to use <> instead of "" and it has certainly made a difference :-)  Now the linker can find the file OK, but it produces a gazillion "Error: Unresolved external . . . " messages referenced from "HelloWorldApp.obj", so the core problem remains.

Thanks again,

Tim Grocott
0
 
DarthNemesisCommented:
Unresolved externals are caused when you have prototype function headers but you never define the bodies. If your 'HelloWorldApp.h' file has some functions defined that it doesn't implement, that would be the problem. If you implemented them, but they're in a file like 'HelloWorldApp.cpp', do the following steps:

(The ifs and define  are just good programming, to prevent a file from being read twice even if it's included twice)
At the top of HelloWorldApp.h add the lines
#IFNDEF _HELLO_WORLD_APP //(use any name that's going to be unique for your file)
#DEFINE _HELLO_WORLD_APP
And at the bottom add
#INCLUDE "HelloWorldApp.cpp"
#ENDIF

This should clear up your unresolved externals.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TimGrocottAuthor Commented:
The header file, which I'm copying from the tutorial manual, already includes lines equivalent to '#IFNDEF . . .' and '#DEFINE . . .'  Here's a copy of the code in case you can spot something that I'm missing:

// 'HelloWorldApp.h'
   #ifndef _HELLOWORLDAPP_H
   #define _HELLOWORLDAPP_H

   /**
   * The HelloWorldApp class
   * This class shows a window containing a statusbar with the text ‘Hello World’
   */

   class HelloWorldApp : public wxApp
   {
   public:
       virtual bool OnInit();
   };

   DECLARE_APP(HelloWorldApp)

   #endif _HELLOWORLDAPP_H


// 'HelloWorldApp.cpp'
   // For compilers that supports precompilation, includes "wx/wx.h"
   #include <wx/wxprec.h>

   #ifndef WX_PRECOMP
       #include <wx/wx.h>
   #endif

   #ifndef WX_PRECOMP
       #include <wx/wx.h>
   #endif

   #include "HelloWorldApp.h"

   IMPLEMENT_APP(HelloWorldApp)

   bool HelloWorldApp::OnInit()
   {
       wxFrame *frame = new wxFrame((wxFrame*) NULL, -1, "Hello World");
       frame->CreateStatusBar();
       frame->SetStatusText("Hello World");
       frame->Show(TRUE);
       SetTopWindow(frame);
       return true;
   }

I tried adding '#INCLUDE <HelloWorldApp.cpp>' before the last '#endif . . .' in HelloWorldApp.h, and this did stop the unresolved external messages, but these are now replaced by messages about multiple declarations and definitions.

I don't think the code is at fault, mainly becuase I didn't write it :-) - at the end of the day it's only a Hello World program!  I think the problem is the way I've installed the wxWindows library, but I can't find any clues in the supplied documentation.  What I need to know is how to go about installing a third party library so the compiler can link it into my programs.

Thank again,

Tim
0
 
DarthNemesisCommented:
This is hard to debug, since I don't have the code in front of me or the platform on which to compile it. From the structure of your HelloWorldApp.cpp, though, it seems that the .cpp file should be included directly by your program rather than the .h file. You can remove the #include I told you to add to the .h file.

Now, your main program has to include HelloWorldApp.cpp. Myself being a Windows user and this being a Linux program, I don't know whether this can be done with an #include "HelloWorldApp.cpp" or just #include "HelloWorldApp".

Is there in fact a "main program" that includes HelloWorldApp, or are you compiling HelloWorldApp.cpp directly?
0
 
TimGrocottAuthor Commented:
The header file, which I'm copying from the tutorial manual, already includes lines equivalent to '#IFNDEF . . .' and '#DEFINE . . .'  Here's a copy of the code in case you can spot something that I'm missing:

// 'HelloWorldApp.h'
   #ifndef _HELLOWORLDAPP_H
   #define _HELLOWORLDAPP_H

   /**
   * The HelloWorldApp class
   * This class shows a window containing a statusbar with the text ‘Hello World’
   */

   class HelloWorldApp : public wxApp
   {
   public:
       virtual bool OnInit();
   };

   DECLARE_APP(HelloWorldApp)

   #endif _HELLOWORLDAPP_H


// 'HelloWorldApp.cpp'
   // For compilers that supports precompilation, includes "wx/wx.h"
   #include <wx/wxprec.h>

   #ifndef WX_PRECOMP
       #include <wx/wx.h>
   #endif

   #ifndef WX_PRECOMP
       #include <wx/wx.h>
   #endif

   #include "HelloWorldApp.h"

   IMPLEMENT_APP(HelloWorldApp)

   bool HelloWorldApp::OnInit()
   {
       wxFrame *frame = new wxFrame((wxFrame*) NULL, -1, "Hello World");
       frame->CreateStatusBar();
       frame->SetStatusText("Hello World");
       frame->Show(TRUE);
       SetTopWindow(frame);
       return true;
   }

I tried adding '#INCLUDE <HelloWorldApp.cpp>' before the last '#endif . . .' in HelloWorldApp.h, and this did stop the unresolved external messages, but these are now replaced by messages about multiple declarations and definitions.

I don't think the code is at fault, mainly becuase I didn't write it :-) - at the end of the day it's only a Hello World program!  I think the problem is the way I've installed the wxWindows library, but I can't find any clues in the supplied documentation.  What I need to know is how to go about installing a third party library so the compiler can link it into my programs.

Thank again,

Tim
0
 
TimGrocottAuthor Commented:
Whoa, sorry - double post.

I should have mentioned that HelloWorldApp.cpp IS the program I'm trying to compile, sorry :-(
0
 
DarthNemesisCommented:
Nevermind about the windows/linux thing... The forward slashes on the include directives threw me off. I'm going to download wxwindows to see what the problem is.
0
 
DarthNemesisCommented:
Wow, the documentation is rather overwhelming... This in particular caught my eye, though:

1. Make sure your WXWIN variable is set [e.g add
   set WXWIN=c:\wxwindows_2.4.0
   to your autoexec.bat file]

They also talk about makefiles repeatedly, but you have to be able to compile the program first before you need to worry about that.
0
 
DarthNemesisCommented:
The full installation instructions can be found in
C:\wxWindows_2.4.0\docs\msw\install.txt under Borland C++ 4.5/5.0/5.5 compilation.
0
 
TimGrocottAuthor Commented:
Whoa, sorry - double post.

I should have mentioned that HelloWorldApp.cpp IS the program I'm trying to compile, sorry :-(
0
 
TimGrocottAuthor Commented:
Holy cow Darth!

Surely this goes above and beyond the call of duty :-)  You're a true gent!

I have followed all of the instructions in install.txt for the Borland compiler, including setting WXWIN in autoexec.bat.  Like I said, I can compile the sample programs from their own source folders by using their makefiles.
0
 
tinchosCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: DarthNemesis

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now