C linker error when using getline - ported from linux to windows

This is a discussion on C linker error when using getline - ported from linux to windows within the C Programming forums, part of the General Programming Boards category; Hello dear fellow coders. As a part of a project I have obtained from a friend some code to read ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    C linker error when using getline - ported from linux to windows

    Hello dear fellow coders.

    As a part of a project I have obtained from a friend some code to read from a file. It was written in C on linux and is confirmed working there. I have got the header and source, but trying to compile on my windows computer running Visual Studio 2008 Pro reports errors.

    I should say that I have only supplied the following code "up till" the point where I get an error message. If the entire code is needed, it will be supplied (in total approx 600 lines)
    First of all the .h file

    Code:
    #ifndef __GAPREAD_H
    #define __GAPREAD_H
    #endif
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    /*-----------------------------------------------------------------------------*/
    int GAPopenfile( char* fname, char** probname );
    /*------------------------------------------------------------------------------
    PURPOSE:
    Opens a file named fname as a "GAPFILE" for reading and returns the "problem name",
    that is the filename without path and extension. 
    
    PARAMETERS:
    - fname   : pointer to an array of chars containing the name of the data file
    - probname: if probname is not NULL on input, the problem's name is
                returned on output in *probname.
    
    RETURN VALUE: 0 on success, 1 if file unable to open file, 2 if error
                  occurred while reading number of instances	    
    -------------------------------------------------------------------------------*/
    
    /*-----------------------------------------------------------------------------*/
    And the the C code until the point of error

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include "gapread.h"
    
    
    
    
    
    static FILE*    GAPfile=NULL;   /* Handle to the data file                     */
    static int      GAPnuminst=0;   /* number of problem instances in input file   */
    static int      GAPcurinst=0;   /* current problem instance                    */
    
    static char*    GAPpname=NULL;  /* problem name                                */
    static double** GAPprofit=NULL; /* GAPprofit[i][j] is the profit of assigning
                                       job j to agent i                            */
    static int**    GAPweight=NULL; /* GAPweight[i][j] is resource requirement
                                       of assigning job j to agent i               */
    static int*     GAPcap=NULL;    /* GAPcap[i] is agent i's capacity             */
    
    static double*  GAPpptr=NULL;   /* pointer to profits of assigning jobs stored */
                                    /* as one array                                */
    
    static int*     GAPwptr=NULL;   /* pointer to weights of assigning jobs stored */
                                    /* as one array                                */
    
    /*-----------------------------------------------------------------------------*/
    
    int GAPopenfile( char* fname, char** probname ) {
    
    /*
      Description:
      ------------
      Opens file named fname as a "GAPFILE" for reading, records the number of
      problem instances contained in the data file and returns the "problem name",
      that is the filename without path and extension.
    
      Parameters:
      ------------
      - fname   : pointer to an array of chars containing the name of the data file
      - probname: pointer to pointer to an array of chars that on output contains
                  the problem name
    
      Return value:
      ------------
      - 0 on success
      - 1 if file fname could not be found
      - 2 if error on reading number of instances has occurred
    
      Scope: Export
      -------------
    */
      char *cptr = strrchr ( fname, (int)'/' );
      char *aline=NULL, *token=NULL;
      const char delim[] = " ";
      int   num, len;
    
      /* Open the data file */
      GAPnuminst = 0;
      GAPcurinst = 0;
      GAPpname   = NULL;
      GAPprofit  = NULL;
      GAPweight  = NULL;
      GAPcap     = NULL;
      if ( probname ) *probname = NULL;
      GAPfile = fopen( fname, "r" );
      if ( GAPfile == NULL ) return( 1 );
    
      /* Get the problem name from the file name */
      if ( cptr == NULL ) cptr = fname; else cptr++;
      GAPpname = strdup( cptr );
    
      /* Check if file has info about number of problem instances in data file */
      aline = (char*) malloc( 1000 );
      num   = getline(&aline,&len,GAPfile);
      token = strtok( aline, delim );
      num   = 0;
      while ( token != NULL ) {
        num++;
        token = strtok ( NULL, delim );
      }
      fclose( GAPfile );
      free(aline);
    
      /* Reopen file and read number of instances */
      GAPfile    = fopen( fname, "r" );
      GAPnuminst = 1;
      if ( num == 1 ) {
        if ( fscanf(GAPfile,"%d\n",&GAPnuminst) != 1 ) return( 2 );
      }
    
      if ( probname ) *probname = GAPpname;
      return( 0 );
    
    }
    I get the error

    Code:
    Compiling is ok
    Linking...
    1>gapread.obj : error LNK2019: unresolved external symbol _getline referenced in function _GAPopenfile
    1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
    1>C:\Documents and Settings\tuec\My Documents\Reader\Debug\Reader.exe : fatal error LNK1120: 2 unresolved externals
    From the 3 hours I have spent googling it could have something to do with the getline not being included/defined. However since it is compiling and running on linux I speculate that it has to do with something else. From what I have read the end line "symbol" for windows is \r\n but for linux it is \n. However I cannot see how this should make the linking part failing.....

    Hopefully I have just done something stupid and the problem is easily solved.

    Kindest regards

    Lagrange

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    If that call to getline is to the *GNU specific* getline (i.e, the GNU implementation) then that is your problem. VS won't know anything about it.

    Edit:

    Some of the others here would be better able to address this (It's been a long time since I have compiled anything on Windows), but it seems to me that if you compiled your program with a Windows version of gcc, you might have some success. Have a look at MinGW, or Cygwin for a way to use gcc in Windows.
    Last edited by kermit; 10-07-2009 at 08:32 AM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    kermit has it on the nose -- getline isn't standard, but GNU likes you and provides it. MinGW uses the MS libs, so I don't believe that will get you any farther.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If worst comes to worst it would not be that hard to write your own version, just consider the prototype:
    Code:
    ssize_t getline (char **lineptr, size_t *n, FILE *stream);
    Then just google the "GNU C Reference" to get an explanation of the functionality.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Well that certainly explains why it doesnt work.

    Is it correct that getline is function supplied in c++ for visual studio?

    I guess my current options is to either to make the code Visual Studio compatible (possible by a small hack) or dualboot with some linux.

    I think I will end up "correcting" the code.

    Thank you all for the great answers!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is a C++ getline, which has roughly nothing whatsoever to do with GNU's getline.

  7. #7
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Lagrange View Post
    Well that certainly explains why it doesnt work.

    Is it correct that getline is function supplied in c++ for visual studio?

    I guess my current options is to either to make the code Visual Studio compatible (possible by a small hack) or dualboot with some linux.

    I think I will end up "correcting" the code.

    Thank you all for the great answers!
    Slax baby!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. windows .dll vs. linux .so - global static objects
    By pheres in forum C++ Programming
    Replies: 11
    Last Post: 11-23-2010, 12:29 PM
  2. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 11:40 AM
  3. Build linux on windows
    By baash05 in forum Linux Programming
    Replies: 6
    Last Post: 02-19-2008, 09:12 PM
  4. Why can't Windows run Linux binary executables?
    By Kleid-0 in forum Tech Board
    Replies: 30
    Last Post: 12-04-2005, 10:44 PM
  5. Convert Windows GUI to Linux GUI
    By BobS0327 in forum Linux Programming
    Replies: 21
    Last Post: 11-27-2005, 03:39 AM

Tags for this Thread


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