Menu

Linker Error Help

kip2847
2008-02-26
2012-09-26
  • kip2847

    kip2847 - 2008-02-26

    Right now I am just fiddling around with c++ but am having a problem.
    To make a long story short am trying to make my program get its functions from another file. I have a main, header , and a function cpp file (these are all in separate files, source files.)

    I understand that there is a easy way of doing this -- File -> New -> Project-> etc etc.
    I am aware of this method. but i dont want to do it this way.

    Like i said as of now i created three differnet source files and save two as .cpp, and one as .h. Also i looked in the FAQ/READ THIS first and tired to follow those direction but it wasn't clear enough for me to get it to work. as you can see in my log i added the -I"C:\dev-cpp\" and -L"C:\dev-cpp\" to linker command but still nothing

    Here is my code:
    // menu.cpp file

    include <iostream>

    include <cstdlib>

    include "menu_options.h"

    using namespace std;
    int main()
    {
    echo();
    cout << " This is the menu test"<<endl;
    echo();
    system("pause");
    echo();
    system("pause");
    }

    //menu_options.h

    if !defined menu_options_h

    define menu_options_h

    void echo();

    endif

    //menu_options.cpp file

    include <iostream>

    include <cstdlib>

    include "menu_options.h"

    using namespace std;
    void echo();
    void echo()
    {
    cout <<"this is a lib."<< endl;
    }

    My error:
    C:\Dev-Cpp\menu.cpp In function int main()': C:\Dev-Cpp\menu.cppecho' undeclared (first use this function)

    This is my log:
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Dev-Cpp\menu_options.cpp" -o "C:\Dev-Cpp\menu_options.exe" -g3 -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -L"C:\Dev-Cpp", -L"C:\Dev-Cpp" -g3
    C:\Dev-Cpp\lib/libmingw32.a(main.o)(.text+0x106):main.c: undefined reference to `WinMain@16'
    collect2: ld returned 1 exit status
    Execution terminated

    Specs-
    Dev C++ 4.9.9.2
    Winxp

     
    • kip2847

      kip2847 - 2008-02-27

      is there a way to do it without creating a new project

       
      • cpns

        cpns - 2008-02-27

        Why would you not want to use a project!?

        There are plenty of ways, all of them less convenient and more error prone than using a project. That is exactly what a project is for - to make this easy for you. Why would you wish to make it harder!?

        I could tell you the ways, but what would be the point? When you got stuck you might then ask for support and I would not wish to support such folly. They all involve not using the IDE to some extent (i.e. using the command line tools). Do you really want to go there? It seems unlikley if you chose to use Dev-C++ in teh first place. If so you can read the manuals, the main ones you need are at:

        http://gcc.gnu.org/onlinedocs/
        http://www.gnu.org/software/binutils/manual/ld-2.9.1/
        http://www.gnu.org/software/make/manual/

        I recommend using a project always, even for single module projects. This is because a project retains all the the compiler and linker settings for the project whereas when you simply load and compile a source file, it will use whatever settings happen to be active in the IDE at that time. All other IDEs I have used only allow you to build using a project; not using a project is error prone. You would do better to ignore the fact that Dev-C++ provides such a facility altogether.

        Clifford

         
    • kip2847

      kip2847 - 2008-02-26

      I was hoping for an answer from cliff or wayne

       
      • cpns

        cpns - 2008-02-26

        Well you only waited four hours! I do have a job too you know, and am almost certainly not in teh same time zone as you.

         
    • cpns

      cpns - 2008-02-26

      Your log and the error you claim you are getting are not from the same build. The first is from an attempt to compile main.cpp, the second is from an attempt to compile menu_options.cpp and link it stand-alone, which won't work as it has no entry point (i.e. main() function).

      You need to compile bith modules and linke them. For that you will need to create a 'project' (File->New->Project, and add the source via teh Project->Add file menu).

      BTW menu_options.cpp is not a 'lib' in the normal sense of the term, it is a 'compilation unit' or 'module' perhaps.

      Clifford

       
    • cpns

      cpns - 2008-02-26

      Oh, and BTW you should remove the extra options you added they are useless.

       

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.