Menu

[Linker error] undefined reference to .....

Sheaker
2008-10-23
2012-09-26
  • Sheaker

    Sheaker - 2008-10-23

    Programme : DeV-Cpp 4.9.9.2

    I've written files funkcja.h and funkcja.c but compilation fails:
    [Linker error] undefined reference to `funkcja(int)'

    [code]

    include <funkcja.h>

    include <iostream>

    main(){
    int a = funkcja(11);
    }

    Console(not in DeV) compilation works ok:
    "g++ main.c funkcja.c"

    but I need to compile it in dev for another programme.

     

    Related

    Code: code

    • Sheaker

      Sheaker - 2008-10-24

      It is very inconfortable for me to create multifile project . Is there any way to make it work like math.h? just include it?

       
      • Wayne Keen

        Wayne Keen - 2008-10-24

        No. Including a header file, and linking a seperate code module (or library) are two very different things.

        Keep in mind a header, in most cases, is just a menu - telling the compiler what functions are available and what their in's and out's are. In most (not all) cases, there is no code.

        Now, in some cases, you could in "include" a source code module, but this defeats the purpose of having separate modules, and doesn't always work.

        You need to get comfortable with projects. Its not hard, just start a project, add the code, and the project takes care of generating the makefile, and doing the build for you. Its really quite sweet.

        Wayne

         
    • cpns

      cpns - 2008-10-24

      > Console(not in DeV) compilation works ok:
      >"g++ main.c funkcja.c"

      That is because in that command line you instructed it to compile and link both main.c and funkcja.c. Take a look at your "Compile Log" in Dev-C++ (which you should have posted). You will see that you have only compiled and linked main.c - you have done nothing to instruct the IDE to compile and link funkcja.a.

      To do this in the IDE you need to use the Project tool. (File->New->Project), and add both main.c and funkcja.c to the project. Then both will be first separately compiled and then linked - which you can observe in teh Compile Log. Note that it does this with three separate invocations of g++ rather than your single command line - this gives greater flexibility, and is better for large projects.

      As an aside, the compiler may determine whether to use C or C++ compilation based on the file extension. You forces C++ compilation by using g++ rather than gcc, but to avoid errors and confusion you should call your files main.cpp and funkcja.cpp

      Clifford

       

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.