C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-07-2009, 08:04 AM   #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
Lagrange is offline   Reply With Quote
Old 10-07-2009, 08:11 AM   #2
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,381
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.
__________________
Got Ed?

sys-sizes

Last edited by kermit; 10-07-2009 at 08:32 AM.
kermit is offline   Reply With Quote
Old 10-07-2009, 11:27 AM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 10,163
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.
tabstop is offline   Reply With Quote
Old 10-07-2009, 11:33 AM   #4
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
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
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Old 10-07-2009, 11:58 AM   #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!
Lagrange is offline   Reply With Quote
Old 10-07-2009, 12:27 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 10,163
There is a C++ getline, which has roughly nothing whatsoever to do with GNU's getline.
tabstop is offline   Reply With Quote
Old 10-07-2009, 01:00 PM   #7
{Jaxom,Imriel,TBD}'s Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 1,047
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!
Kennedy is offline   Reply With Quote
Reply

Tags
getline, linker error, lnk2019

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Thinking of upgrading to linux... Yarin General Discussions 37 07-24-2009 11:40 AM
Build linux on windows baash05 Linux Programming 6 02-19-2008 10:12 PM
windows .dll vs. linux .so - global static objects pheres C++ Programming 6 08-22-2007 12:07 PM
Why can't Windows run Linux binary executables? Kleid-0 Tech Board 30 12-04-2005 11:44 PM
Convert Windows GUI to Linux GUI BobS0327 Linux Programming 21 11-27-2005 04:39 AM


All times are GMT -6. The time now is 08:27 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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