Thread: Why "unresolved external"?

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    Okays, I tried it (finally) again but still didn't get it to work. Nor compilers, linkers or anything else. I noticed that even pragma didn't work now, it just seemed to fix "unresolved externals" of constructor and destructor... It didn't work when I added one normal function...gave unresolved again I read about makefile but I didn't find a way to edit it, it was just readable.

    As I said above, if you post all of your updated files and your exact compiler error/warnings, then I can give it a try on my side to see if I can get it working, without things like "pragma", etc.
    So, here are my 3 files excatly how they are, and errors by running the program (compile & link):

    Project1.cpp:
    Code:
    #include "a.h"
    
    int main() {
       A b;
       b.draw();
       getch();
    }
    a.h:
    Code:
    #ifndef A_H
    #define A_H
    
    #include <conio.h>
    #include <stdio.h>
    
    class A {
    public:
       A();
       ~A();
    
       void draw();
       
    private:
       int o;
    };
    
    #endif
    a.cpp:
    Code:
    #ifndef A_CPP
    #define A_CPP
    
    #include "a.h"
    
    A::A() {
       o = 3;
    }
    A::~A() {}
    
    void A::draw() {
       printf("%i", o);
    }
    
    #endif
    Errors:
    [Linker Error] Unresolved external 'A::~A()' referenced from D:\PELIT\FOREST - OBJ\PROJECT1.OBJ.
    [Linker Error] Unresolved external 'A::A()' referenced from D:\PELIT\FOREST - OBJ\PROJECT1.OBJ.
    [Linker Error] Unresolved external 'A::draw()' referenced from D:\PELIT\FOREST - OBJ\PROJECT1.OBJ.

    In a way I hope you'll get those errors too or there is something else wrong?

  2. #17
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The only problems I had is with, in project1.cpp, "getch()". I commented out this line, as I dont think this is a standard C++ call, however I dont think you have any problems with this on your platform and compiler (or at least you dont care that its not standard).

    The next problem I had is that "conio.h" isnt standard either, and Im on Linux and I think this is only for Windows. So I dont think you have a problem with that file either.

    After commenting the two questionable lines (getch and conio), I was able to (fully) compile and run the program without problems. I still dont think you mentioned what compiler your using, or even if you did, I dont think you mentioned how your compiling it, but my guess is your only compiling one of the 2 .cpp files. If you try and compile only project1, it will complain that it cant find the various functions (similar to your current errors). If you only compile a.cpp, it will complain it cant find main, because it doesnt exist. Compile both .cpp files at the same time.

  3. #18
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I had the same results (also on Linux); loose the conio.h, getch() (replaced it with a cin >> foo) and used the following CMakeLists file which looks like this:
    CMakeLists.txt:
    Code:
    cmake_minimum_required(VERSION 2.6)
    PROJECT(unresolved_test)
    ADD_DEFINITIONS( -Wall -ggdb )
    ADD_EXECUTABLE(testapp project.cpp a.cpp)
    TARGET_LINK_LIBRARIES(testapp )
    And it builds and runs fine on both Linux and Windows.

    Query: What is the intention of the guards on the file a.cpp? The guards on the header keep it from being included twice (redef build errors) but on a .cpp? Just curious...

    Peace...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    The only problems I had is with, in project1.cpp, "getch()". I commented out this line, as I dont think this is a standard C++ call, however I dont think you have any problems with this on your platform and compiler (or at least you dont care that its not standard).
    Yes, I put C code in it because it was easier (and shouldn't affect errors in this case?). I haven't used iostreams so much and couldn't remember how to use it.

    After commenting the two questionable lines (getch and conio), I was able to (fully) compile and run the program without problems. I still dont think you mentioned what compiler your using, or even if you did, I dont think you mentioned how your compiling it, but my guess is your only compiling one of the 2 .cpp files. If you try and compile only project1, it will complain that it cant find the various functions (similar to your current errors). If you only compile a.cpp, it will complain it cant find main, because it doesnt exist. Compile both .cpp files at the same time.
    I removed the guards in cpp file like jeffcobb said

    I use Borland C++ Builder 4.0. I have always ran all programs via, IDE, its own "run" button. I just write code and run it. I found in Borland's help that "running" means making, then running the exe file. I also found my "makefile". It's the same as project file (.bpr) but changing it didn't remove these errors. The makefile looks like this:
    Code:
    # ---------------------------------------------------------------------------
    !if !$d(BCB)
    BCB = $(MAKEDIR)\..
    !endif
    
    # ---------------------------------------------------------------------------
    # IDE SECTION
    # ---------------------------------------------------------------------------
    # The following section of the project makefile is managed by the BCB IDE.
    # It is recommended to use the IDE to change any of the values in this
    # section.
    # ---------------------------------------------------------------------------
    
    VERSION = BCB.04.04
    # ---------------------------------------------------------------------------
    PROJECT = Project1.exe
    OBJFILES = Project1.obj
    RESFILES =
    RESDEPEN = $(RESFILES)
    LIBFILES =
    IDLFILES =
    IDLGENFILES =
    LIBRARIES = 
    SPARELIBS = 
    PACKAGES = Vcl40.bpi Vclx40.bpi bcbsmp40.bpi dclocx40.bpi
    DEFFILE =
    RESDEPEN = $(RESFILES)
    # ---------------------------------------------------------------------------
    PATHCPP = .;
    PATHASM = .;
    PATHPAS = .;
    PATHRC = .;
    DEBUGLIBPATH = $(BCB)\lib\debug
    RELEASELIBPATH = $(BCB)\lib\release
    SYSDEFINES = _NO_VCL
    USERDEFINES =
    # ---------------------------------------------------------------------------
    CFLAG1 = -I$(BCB)\include -Od -w -Tkh30000 -r- -a8 -k -y -v -vi- -c -tWM -tWC -D$(SYSDEFINES);$(USERDEFINES)
    IDLCFLAGS = -src_suffixcpp
    PFLAGS = -U$(BCB)\lib;$(DEBUGLIBPATH) -I$(BCB)\include -$YD -$W -$O- -v -JPHN -M
    RFLAGS = -i$(BCB)\include
    AFLAGS = /i$(BCB)\include /mx /w2 /zd
    LFLAGS = -L$(BCB)\lib;$(DEBUGLIBPATH) -ap -Tpe -x -Gn -v
    # ---------------------------------------------------------------------------
    ALLOBJ = c0x32.obj $(OBJFILES)
    ALLRES = $(RESFILES)
    ALLLIB = $(LIBFILES)  import32.lib cw32mt.lib
    # ---------------------------------------------------------------------------
    !ifdef IDEOPTIONS
    I couldn't find a way to add text to "OBJFILES".

    Simple running via IDE has always worked before... During this "talking" I've tried to use compiler and linker that I found in my Borland directory. Compiler is "bcc32.exe" and linker "ilink32".exe. It's just damn hard to use those, partly because preprocessors aren't seemingly used so I haven't been able to include anything.

    The reason that I'm trying to move from C to C++ is because my old C program grew too large and it got too messy. I think now that I'll use C++, it is easier to plan and do with it. If it likes to work sometimes
    Last edited by JulleH; 12-16-2009 at 09:34 AM.

  5. #20
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Try and run it from the command line. A simple search brought up the command line tool that the IDE (and makefile) uses to compile--you should already have this. This is the link I looked at Using the Borland 5.5 Compiler and command-line tools (of course it says v5.5, but the same general thing must apply to 4.0). Then open your command prompt and "cd" to the bin directory where bcc32.exe is, as the link says. Then do something as simple as
    Code:
    bcc32 a.cpp project1.cpp
    and see if it creates an .exe for you. If you get errors, try and specify the default include and lib paths (of course these must match your installation so change as necessary)
    Code:
    bcc32 -If:\Borland\bcc55\include -Lf:\Borland\bcc55\Lib a.cpp project1.cpp
    If you still get errors then, at least for the lib, change the lib path to wherever your lib files are that are mentioned in your makefile ("import32.lib cw32mt.lib
    "). I dont think those are even necessary, but well see.

  6. #21
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    finally something good...it works...via command line, even pretty soon. Can't do it via IDE, though. I tried to mess around with makefile of my project (project1.bpr). I thought it'd work after changing library and include (default) directories to very same as my Borland directory, not like it was, "$(BCB)\include" and so on. Then for example "$(BCB)\include" was substituted by the default include directory. So I think that "CFLAG1" and "LFLAGS" are the important parts of this issue:
    Code:
    CFLAG1 = -I$(BCB)\include -Od -w -Tkh30000 -r- -a8 -k -y -v -vi- -D$(SYSDEFINES);$(USERDEFINES)
    IDLCFLAGS = -src_suffixcpp
    PFLAGS = -U$(BCB)\lib;$(DEBUGLIBPATH) -I$(BCB)\include -$YD -$W -$O- -v \-JPHN -M
    RFLAGS = -i$(BCB)\include
    AFLAGS = /i$(BCB)\include /mx /w2 /zd
    LFLAGS = -L$(BCB)\lib;$(DEBUGLIBPATH) -ap -Tpe -x -Gn $(BCB)\lib -v;$(DEBUGLIBPATH)
    Arguments are very messy... I just hope it would just work with IDE, otherwise it seems ok
    Last edited by JulleH; 12-16-2009 at 01:04 PM.

  7. #22
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    The command line, in general (i.e. not just for compilers), are usually easier to use and more flexible, as you have full control. An IDE isnt a compiler, its basic form is a GUI text editor, with a button to compile the code. The "button" basically calls the back end--the command line compiler--and passes some arguments. So for the IDE to be as powerful as the command line compiler itself, it must allow for all options and combinations of options to the compiler, which means it takes a lot of work to implement (its almost like writing a parser for the compilers options).

    If your using a makefile already, you might as well just use a text editor and whenever you want to compile, you run a script that compiles with the makefile or any other options. Or you could just write a simple script to call bcc32 itself--not really any need for a makefile for simple projects. Ive never used one for school or non-work projects (even for work, Ive never written or modified one). For example, do you know exactly what all the lines in the makefile do? I certainly dont. However, I know exactly what the command line arguments do, so I have more control of what I want it to do and what its doing. An IDE or makefile might do other stuff, or assume things for you that might not be correct or what you want.

    If you still cant get it to work with the IDE, I would suggest to create an entirely new "project". You of course keep your source code files, but you just create a new "project". Then include all your source files again, and pay attention to any options/configurations it gives you when you include or try to compile. Maybe theres an option to use all .cpp files when you run/build/whatever goofy word it uses, and right now your only compiling one of the .cpp files (which is what it seems).

    Im not saying dont ever use an IDE. Microsoft Visual Studio (or in particular, the free version of Microsoft Visual C++) are extremely high quality products. I havent checked lately, but Ive always thought that Borland is "outdated" software. So, again, if you cant get it working in your IDE, try the free version of Visual C++ or some other popular IDE.
    Last edited by nadroj; 12-16-2009 at 01:33 PM.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    I've learned a lot from this...thanks Now I can continue my programs (until...). Anyway, this was reeealy good idea to ask this in here. There are experts in here and don't have to wait answers forever. I'll try that freeware MVC++, asap...even if I can make it work via Borland.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-27-2006, 10:39 AM
  2. The Dreaded "Unresolved External Symbol" :-(
    By DrMario in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2005, 01:43 AM
  3. Ask about these "unresolved external symbol" error
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2002, 11:39 AM
  4. What's an "unresolved external"
    By Clane in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2002, 09:23 AM