Thread: Proper library inclusion - problem

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    4

    Proper library inclusion - problem

    Hello guys!

    I have these 3 files:

    Code:
    //main.cpp
    
    #include "lib.h"
    
    
    int main ()
    {
        foo();
        return 0;
    }
    Code:
    //lib.h
    
    void foo();
    Code:
    //lib.cpp
    
    #include "lib.h"
    #include <iostream>
    
    
    void foo()
    {
        std::cout<<"it works!";
    }
    and in Dev-C++ I get an error that says: "undefined reference to foo".
    I know it's very simple, am I missing something in the code? Should I compile/link them manually from console?

    Note: I read that this is the recommended way to write/include a libray, but I still don't understand how lib.cpp gets to be used in main.cpp as main.cpp only has a reference of lib.h, and lib.h doesn't say anything about the source file (lib.c).

    Thanks!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to compile both main.cpp and lib.cpp. The linker takes the output from the compiler of both these and puts them together into one file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    4
    I can't compile lib.cpp because I get this error: "c:\crossdev\src\mingw-w64-svn\mingw-w64-crt\crt\crt0_c.c undefined reference to `WinMain'"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    g++ main.cpp lib.cpp -o myapp.exe
    Do not compile separately.
    If you are compiling from your IDE, ensure you are writing a console app, not a windows app.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2014
    Posts
    4
    Thanks, somehow I managed to use the cl command instead of g++, but only in an (elevated) window that had previously run "vcvarsall.bat x86". Any other one gives me the error that says "cl is not recognized as an int/ext command".

    Still, some things are not clear to me:why have I got all these errors in Dev?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am not familiar with Dev-C++, but perhaps you did not add all your source files to your project.
    cl is also the executable name for Microsoft's C++ compiler, shipped with the Visual Studio IDE.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jul 2014
    Posts
    4
    Thanks a lot!

    Apparently, I should have created a project in which all files should have been added I knew it was too simple to be a code problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying Proper Value With Arrays
    By joshrod921 in forum C Programming
    Replies: 4
    Last Post: 06-30-2014, 12:51 PM
  2. Replies: 3
    Last Post: 04-13-2012, 07:50 PM
  3. biggest proper problem
    By yogibear1 in forum C Programming
    Replies: 0
    Last Post: 12-16-2009, 02:39 PM
  4. inclusion guard help
    By romerboy55 in forum C++ Programming
    Replies: 3
    Last Post: 09-02-2006, 12:39 AM
  5. class and header inclusion problem.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2002, 10:12 PM

Tags for this Thread