Menu

[Linker error] undefined reference to ....

leffers
2009-02-12
2012-09-26
  • leffers

    leffers - 2009-02-12

    I’m trying to take up programming in C again after 15 years with other languages, so I like to apologize in advance if it’s just at stupid error on my side.
    This is the first time I’m trying Dev-c++, so it may just be a simple well known problem, that has already been answered in an old thread (I couldn’t find it). In that case I’m sorry to take your time.

    Her is the problem in the shortest possible form: I have just 3 very small files, all in the same directory:

    //main.c

    include <stdio.h>

    include <stdlib.h>

    include "myprint.h"

    int main(int argc, char *argv[])
    {
    myprint();
    system("PAUSE");
    return 0;
    }myprint.c


    //myprint.c

    include <stdio.h>

    include <stdlib.h>

    void myprint(void){
    puts("from myprint");
    return;
    }
    i-----------------------------------
    // myprint.h
    void myprint(void);

    I created a new project consisting of the two c-files, and when I try to compile it, I get the following error-message:
    [Linker error] undefined reference to ‘myprint()’

     
    • cpns

      cpns - 2009-02-15

      > I have used it for small projects in the past

      Perhaps that is why you don't perhaps see teh benefit. C++ (or at least OOD/OOP) exhibit significant benefits in larger projects.

      > I don’s want to rewrite my code

      Shouldn't need to. Most well formed C code will compile as C++, and the C++ compiler has stronger error checking so may find some problems you did not know where there. One approach is to leave the code as it is but recompile it as C++, then in extending and maintaining the code, you have the opportunity to make use of C++ features that are of immediate benefit, and to refactor some parts as you go.

      That said, just because you can does not mean you should! I think you are probably right to be cautious.

      Clifford

       
    • Wayne Keen

      Wayne Keen - 2009-02-12

      Could you post your full compile log? It's on the tab labeled
      "Compile Log" and the right mouse button brings up the copy menu

      Wayne

       
    • cpns

      cpns - 2009-02-12

      > I’m trying to take up programming in C again after
      > 15 years with other languages

      Why!? In the meantime C++ has passed it by.

      > This is the first time I’m trying Dev-c++

      Don't make it hard for yourself. Get a better tool with a better debugger. I am more than willing to support users of Dev-C++ who have made an informed choice, or have legacy projects to maintain, but in most cases the informed choice would be something else if you are just starting out.

      > [Linker error] undefined reference to ‘myprint()’

      To get Dev-C++ (or any other IDE) to perform separate compilation an linking, you have to create a project, and add the sources to the file. Without a project, Dev-C++ merely compiles and links the currently selected file.

      File->New->Project

      Clifford

       
    • TexasOwl

      TexasOwl - 2009-02-12

      Clifford
      I read your comment with great interest.
      I am totaly new to programing in C++ after programing in MAATLAB for several years.

      I have no idea what a good tool is.
      Do you have any suggestions?

      What do you think of visual studio?

       
      • Wayne Keen

        Wayne Keen - 2009-02-12

        Given the fact that he plugs it a couple of times a week, I think he is OK with it.

        ;)

         
        • Wayne Keen

          Wayne Keen - 2009-02-12

          By the way, that wasn't a reflex question that I asked, having your log is important, that is why it is in the "Basic 3"

          Wayne

           
    • leffers

      leffers - 2009-02-13

      To Wayne Keen:

      Here is the Compile Log
      Kompiler: Default compiler
      Building Makefile: "C:\Users\Jan\jale\Makefile.win"
      Eksekverer make clean
      rm -f MAIN.o myprint.o jale.exe

      gcc.exe -c MAIN.C -o MAIN.o -I"C:/Dev-Cpp/include"

      gcc.exe -c myprint.c -o myprint.o -I"C:/Dev-Cpp/include"

      gcc.exe MAIN.o myprint.o -o "jale.exe" -L"C:/Dev-Cpp/lib"

      MAIN.o(.text+0x2b):MAIN.C: undefined reference to `myprint()'
      collect2: ld returned 1 exit status

      make.exe: *** [jale.exe] Error 1

      Eksekvering afbrudt

      TO cpns:

      Why C? Well because I wrote to chess-playing program many years ago (as a hobby) and I want to work a little with it again, just for fun. I have done some programming in C++, and perhaps I will rewrite some of the code to C++, but there is no good reason to do it (I think), because speed is the key-word chess programming, so there is no need for most of the fancy stuff in C++.

      And I have created a Project and added the files to it. I am not that stupid :-)

       
    • cpns

      cpns - 2009-02-14

      > I am totaly new to programing in C++ after
      > programing in MAATLAB for several years.

      Perhaps I'll be asking for your help soon, I am just about to start Matlab.

      > What do you think of visual studio?

      It has the best available debugger. If you are not using a debugger you should be, however the Dev-C++ debugger is unusable. VC++ 2008 Express Edition is free and far more powerful than Dev-C++. Visual Studio tends to add 'helpful' extras when you use the app wizards, which for console apps are mostly unnecessary, and only serve to make your code non-portable. I always remove all that stuff, and the advantages of things like pre-compiled headers are minimal for moderately sized projects.

      > Here is the Compile Log

      Ok it seems that you have compiled an linked two separate modules. For main.o to have compiled, it must have seen a prototype for myprint(), but to get the linker error it must be the case then neither module actually contains a definition for myprint(). However the code you posted clearly shown it being there. And you are using C compilation throughout (mixing C and C++ can cause similar problems). Very strange.

      The only suggestion that I can come up with is that the myprint.c file you posted is not teh one that you are compiling an linking. Check its path in the file properties in the project pane. Do you have more than one file with that name in separate folders perhaps. That is the only hypothesis that seems to make sense at the moment.

      > Well because I wrote to chess-playing program many years
      > ago (as a hobby) and I want to work a little with it again

      Legacy code is one good reason to use C

      > because speed is the key-word chess programming

      You think C++ is slower than C!? Many people are under than misaprehension. C++ provides additional constructs not in C that in some cases present an overhead, but the overhead is often insignificant, and you are not obliged to use those constructs. Using classes and objects of itself does not present an overhead. C++ also provides a more extensive class library; while it may be true that an algorithm using an array might be faster than one using a <vector>, if you needed the functionality and safety of a <vector> and coded it yourself in C, it is likely to have the same or worse performance than the carefully constructed library routine. String handling in C++ using teh std::string class is likely to be faster than using the C string library for teh reasons described here: http://www.joelonsoftware.com/articles/fog0000000319.html

      > And I have created a Project and added the files to it. I am not that stupid :-)

      I see that from teh log, which is why we ask for it. It saves the "have you plugged it in?" type counter-question, and removes all ambiguity, and often allows you to provide very little additional information. People often make assertions that turn out to be false. Many people posting here have made the mistake I described. Until we know you, we don't know how smart you are. You seem smart enough, but you also have to realise that we are attempting to assist by remote, and clear accurate information is essential.

      Clifford

       
    • leffers

      leffers - 2009-02-14

      I have now created a new project in a new directory. Created three new files, renamed the files to the same as the old file, and copied (cut and paste) the code from the old files into the new files, and it works :-).
      I have tried a lot of other tings, so perhaps it was a combination of strange circumstances.

      Anyway thanks for your help.

      And perhaps I should take up C++ again (I have used it for small projects in the past), but I don’t want to mix languages and I don’s want to rewrite my code, so it must be in a other project.

      Regards
      Jan Leffers

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.