Problem compiling files that store functions

This is a discussion on Problem compiling files that store functions within the C++ Programming forums, part of the General Programming Boards category; I'm at a chapter on C++ where it teaches how to create a function and how codes can be separated ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    18

    Problem compiling files that store functions

    I'm at a chapter on C++ where it teaches how to create a function and how codes can be separated into several files. Well, in a file that define a function in it, I'm having a problem compiling (actually, I have problems compiling for all of them). I believe I copy the codes correctly. Anyway, when I try to compile my file, I would get this error:

    Code:
    C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\libmingw32.a(main.o):main.c:(.text+0x104)||undefined reference to `_WinMain@16'|
    ||=== Build finished: 1 errors, 0 warnings ===|
    As the error message should indicate, I'm using Code::Blocks IDE.
    Anyway, this is one of the files I'm trying to compile:

    The header named median.h
    Code:
    #ifndef GUARD_median_h
    #define GUARD_median_h
    
    // median.h--final version
    #include <vector>
    double median(std::vector<double>);
    
    #endif
    And here's the part of the code in a file named median.cpp
    Code:
    #include <algorithm>
    #include <stdexcept>
    #include <vector>
    #include "median.h"
    
    using std::domain_error;     using std::sort;     using std::vector;
    
    //compute the median of a vector<double>
    double median(vector<double> vec)
    {
        typedef vector<double>::size_type vec_sz;
    
        vec_sz size = vec.size();
        if (size == 0)
            throw domain_error("median of an empty vector");
    
        sort(vec.begin(), vec.end());
    
        vec_sz mid = size/2;
    
        return size &#37; 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
    }
    I tried adding int main() after a bit of google, but I received a different error instead...
    What am I doing wrong?
    Last edited by tiachopvutru; 05-30-2008 at 02:48 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure what you are actully trying to achieve. If it's just a project with multiple files, then all you need is another file with a main in it.

    If you are trying to build a library of some kind, then you probably need some different settings.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    18
    Quote Originally Posted by matsp View Post
    Not sure what you are actully trying to achieve. If it's just a project with multiple files, then all you need is another file with a main in it.

    If you are trying to build a library of some kind, then you probably need some different settings.

    --
    Mats
    I'm just copying the code from the book to run and see what it does (plus, it's the Exercise #1 of this chapter). The chapter I'm doing, which is chapter 4, is teaching how to create functions to solve certain problems and make it easier. There are some other files beside the one I just posted, but the program in this chapter is a grading program (again). The median function above is used to calculate the median of all the students' homework grades.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, I just looked at your error message again: It looks like it's expecting there to be a "WinMain", which is the normal starting point for a windows application. You probably should configure your project.

    In dev-cpp, you can change the project settings in "Project Options" - select Windows Console as the project type, instead of the "Windows GUI".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    18
    Quote Originally Posted by matsp View Post
    Actually, I just looked at your error message again: It looks like it's expecting there to be a "WinMain", which is the normal starting point for a windows application. You probably should configure your project.

    In dev-cpp, you can change the project settings in "Project Options" - select Windows Console as the project type, instead of the "Windows GUI".

    --
    Mats
    How do you do that in Code::Blocks? >_<

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Probably nearly the same way, but I haven't used Code::Blocks yet. Downloading right now.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    You aren't qualifying your references to vector as std::vector -- you do it in the header file but not the implementation file.

    EDIT: Oops -- that "using std::vector" was hiding from me.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And to change the type of project, you right-click on your project (probably the second thing down in the project view to the left), and select "Properties...". Then select the "Build targets" tab, and Select "Console application" in "Type" on the right-hand side.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    18
    Quote Originally Posted by matsp View Post
    And to change the type of project, you right-click on your project (probably the second thing down in the project view to the left), and select "Properties...". Then select the "Build targets" tab, and Select "Console application" in "Type" on the right-hand side.

    --
    Mats
    You mean the Project Tab under "Management" on the left side of the window? If so, I don't see it...

    It might have to do something with how I create my "project." I actually wrote this code in when I created "New File" and not "New Project."

    Anyway, I created a New Empty Project and did what you instructed, but when I compile the median file (and others), I got this:

    Code:
    -------------- Build: Debug in Score_grader ---------------
    
    Linking stage skipped (build target has no object files to link)
    Nothing to be done.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    After you create the new empty project, you have to add the source files to the project. Right-click on the project name over on the left, do "add existing..." (I think) and add all the source code files you desire.

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    18
    Ah, it works now. Thank you for all the helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with function's pointer!
    By Tirania in forum C Programming
    Replies: 5
    Last Post: 11-28-2008, 03:50 AM
  2. gcc compiling and linking problem
    By maven in forum C Programming
    Replies: 6
    Last Post: 11-14-2008, 04:28 AM
  3. Problem with Header files.
    By chakra in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 01:30 PM
  4. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 03:55 AM
  5. problem reading files in C
    By angelfly in forum C Programming
    Replies: 9
    Last Post: 10-10-2001, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21