LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-19-2005, 02:03 PM   #1
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Getting undefined reference to main error??


I wrote the following subclass for my main program:

Code:
#include <stdio.h>
#include <stdlib.h>
#include "function.h"

#define READFILE "r"
#define WRITEFILE "w"
#define WORDARRAYSIZE 16
#define MAXIMUMWORDCHARACTERS 15

void CreateWordList (char inputFileName [ ])
{
   FILE* inputFile;
   
   inputFile = fopen (inputFileName, WRITEFILE);
   
   if (inputFile == NULL)
   {
      fprintf(stderr, "Error - Cannot open %s for reading!\n", 
	      inputFileName);
      
      exit (-1);
   }
}

/****
* CountNumberOfWords
*
****/

int CountNumberOfWords (FILE* inputFile)
{
   int wordCounter = 0;
   char wordArray [WORDARRAYSIZE];
   
   while (fgets (wordArray, WORDARRAYSIZE, inputFile) != NULL )
   {
      wordCounter++;
   }
   
   return wordCounter;
}
Basically, im just trying to count the number of words in a data file, where you have no idea how many words are actually there. The first function has not been completed, and is completely useless in its remedial stages, but somewhere, the following error occurs:

Code:
/usr/lib/crt1.o: In function `_start':
/usr/lib/crt1.o(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
My first guess is a library issue, but im not sure? Any ideas as to why this error occurs?

Thanks a ton!
Mistro116
 
Old 11-19-2005, 02:13 PM   #2
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
Is this the entire code? If so, what are you trying to learn C from? Its telling you exactly what is wrong. Where is your main function?
 
0 members found this post helpful.
Old 11-19-2005, 02:22 PM   #3
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
the main function is simply:

Code:
#include <stdio.h>
#include <stdlib.h>

void CreateWordList (char inputFileName [ ]);

int CountNumberOfWords (FILE* inputFile);

int main(int numberOfCommandLineArguments, 
	 char *commandLineArguments [ ])
{
   if (numberOfCommandLineArguments != 4)
   {
      fprintf (stderr, "\nError - ");
      fprintf (stderr, "Incorrect number of command ");
      fprintf (stderr, "line arguments!\n\n");
      fprintf (stderr, "The user must enter command line ");
      fprintf (stderr, "arguments in the following order:\n\n");
      fprintf (stderr, "(Name of Executable File), ");
      fprintf (stderr, "(Name of File Containing the Puzzle ");
      fprintf (stderr, "Letters),\n(Name of File Containing ");
      fprintf (stderr, "the List of Words to Try to Find), ");
      fprintf (stderr, "and\n(Name of Output File in Which ");
      fprintf (stderr, "to Write the Answers)\n\n");
      fprintf (stderr, "Please try again!\n\n");
      
      exit (-1);
   }
   
   CreateWordList (commandLineArguments [2]);
   
   return 0;
}
But this should have no impact on the error because I am simply doing a gcc -Wall -ansi of function.c, which are the functions posted on my last post. The main function is simply doing nothing at this point, but checking for command line arguments.

Any other ideas for this undefined reference???

Thanks.

Mistro116
 
Old 11-19-2005, 02:41 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
It doesnt look like everything is in the same file... .. right?

And it looks like you may have main() in the header file and the function defs in the source (.c*) file.. If this is true thats your problem...
 
Old 11-19-2005, 05:18 PM   #5
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
Quote:
Any other ideas for this undefined reference???
Go back to basics and read Section 1.1 of The C Programming Language by K&R.

I'm courious, what has happened to 'argc' and 'argv'?
 
Old 11-19-2005, 05:52 PM   #6
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
Smile

Variable names are not interdependent on execution. It's the reason in java for variances in the static main function, which allows it to compile universally without problems.

For instance, static main(String [ ] args) can be the same as static main(String[ ] anyvariablename), and will work without a problem.

Think of it like changing basic variable names, they are just parameters in a function, which happens to be main, so thats not the problem.

Would there be any issue without passing this function as integer, I got it to work as a void function, and im returning everything correctly and such. It's kind of awkward. Any ideas?

Thanks.
Mistro116
 
Old 11-19-2005, 06:15 PM   #7
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 213

Rep: Reputation: 31
I need your "function.h" please.
 
Old 11-19-2005, 08:31 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
I'm guessing your main is in a separate .c/.cpp file. There are a couple of steps in building an executable with C/C++. The first step compiles to object files. The 2nd step links those into the final binary. Your problem is that you do not have the object file which contains your main function, so the linker is giving you an undefined reference error. Now, assuming that your main is in main.c, just add main.c to your command line parameters. Typically the 2 step process is hidden in these cases.

If for some reason you wanted to do the 2 steps yourself, you could use the -c parameter. That will create the intermediate .o files, and then you can link the .o files together with an additional call to gcc/g++.

e.g.

Code:
gcc -c file1.c -o file1.o
gcc -c file2.c -o file2.o
gcc file1.o file2.o -o executable

or just

gcc file1.c file2.c -o executable

This differs from Java, where you can just call javac on a single .java file and the javac compiler will find all other files that java file is dependent upon.

Last edited by deiussum; 11-19-2005 at 08:33 PM.
 
Old 11-23-2005, 03:37 PM   #9
pplppp
LQ Newbie
 
Registered: Jul 2004
Posts: 3

Rep: Reputation: 0
what is the command you use to run gcc (what options you give)?
what is the version of gcc you have?
 
Old 11-23-2005, 04:27 PM   #10
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Original Poster
Rep: Reputation: 15
This problem was taking care of, it was a pre-post to the read number of lines post. It basically involved library issues and other small things like that!

But thanks anways.

Mistro116
 
Old 05-23-2009, 06:45 AM   #11
akhil999in
LQ Newbie
 
Registered: Nov 2006
Location: Bombay, not Calcutta
Distribution: FC14
Posts: 2

Rep: Reputation: 0
Thumbs up (.text+0x18): undefined reference to `main'

(.text+0x18): undefined reference to `main'

I found one unorthodox solution by copy pasting the entire contents of main.cpp into the conversionform.ui.h file of the application, given below after pasting it:-



/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

/*1) conversionform.ui.h

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/

#include <qvalidator.h>

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/* Pasted from main.cpp:

#include <qapplication.h>
#include "conversionform.h"

int main( int argc, char ** argv )
{
QApplication a( argc, argv );
ConversionForm w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}


/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
/* Original conversionform.ui.h:


void ConversionForm::convert()
{
enum MetricUnits {
Kilometers,
Meters,
Centimeters,
Millimeters
} ;
enum OldUnits {
Miles,
Yards,
Feet,
Inches
} ;

// Retrieve the input
double input = numberLineEdit ->text().toDouble();
double scaledInput = input;

// internally convert the input to millimeters
switch ( fromComboBox->currentItem() ) {
case Kilometers:
scaledInput *= 1000000 ;
break ;
case Meters:
scaledInput *= 1000 ;
break ;
case Centimeters:
scaledInput *= 10 ;
break ;
}

//convert to inches
double result = scaledInput * 0.0393701 ;

switch ( toComboBox->currentItem() ) {
case Miles:
result /= 63360 ;
break ;
case Yards:
result /= 36 ;
break ;
case Feet:
result /= 12 ;
break ;
}

// set the result
int decimals = decimalsSpinBox->value() ;
resultLineEdit->setText ( QString::number ( result, 'f', decimals ) ) ;
numberLineEdit->setText ( QString::number ( input, 'f', decimals ) ) ;

}

void ConversionForm::init ()
{
numberLineEdit->setValidator ( new QDoubleValidator ( numberLineEdit ) ) ;
numberLineEdit->setText ( "10" ) ;
convert () ;
numberLineEdit->selectAll () ;
}

{
QApplication a( argc, argv );
ConversionForm w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}



void ConversionForm::convert()
{
enum MetricUnits {
Kilometers,
Meters,
Centimeters,
Millimeters
} ;
enum OldUnits {
Miles,
Yards,
Feet,
Inches
} ;

// Retrieve the input
double input = numberLineEdit ->text().toDouble();
double scaledInput = input;

// internally convert the input to millimeters
switch ( fromComboBox->currentItem() ) {
case Kilometers:
scaledInput *= 1000000 ;
break ;
case Meters:
scaledInput *= 1000 ;
break ;
case Centimeters:
scaledInput *= 10 ;
break ;
}

//convert to inches
double result = scaledInput * 0.0393701 ;

switch ( toComboBox->currentItem() ) {
case Miles:
result /= 63360 ;
break ;
case Yards:
result /= 36 ;
break ;
case Feet:
result /= 12 ;
break ;
}

// set the result
int decimals = decimalsSpinBox->value() ;
resultLineEdit->setText ( QString::number ( result, 'f', decimals ) ) ;
numberLineEdit->setText ( QString::number ( input, 'f', decimals ) ) ;

}

void ConversionForm::init ()
{
numberLineEdit->setValidator ( new QDoubleValidator ( numberLineEdit ) ) ;
numberLineEdit->setText ( "10" ) ;
convert () ;
numberLineEdit->selectAll () ;
}

{
QApplication a( argc, argv );
ConversionForm w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}



void ConversionForm::convert()
{
enum MetricUnits {
Kilometers,
Meters,
Centimeters,
Millimeters
} ;
enum OldUnits {
Miles,
Yards,
Feet,
Inches
} ;

// Retrieve the input
double input = numberLineEdit ->text().toDouble();
double scaledInput = input;

// internally convert the input to millimeters
switch ( fromComboBox->currentItem() ) {
case Kilometers:
scaledInput *= 1000000 ;
break ;
case Meters:
scaledInput *= 1000 ;
break ;
case Centimeters:
scaledInput *= 10 ;
break ;
}

//convert to inches
double result = scaledInput * 0.0393701 ;

switch ( toComboBox->currentItem() ) {
case Miles:
result /= 63360 ;
break ;
case Yards:
result /= 36 ;
break ;
case Feet:
result /= 12 ;
break ;
}

// set the result
int decimals = decimalsSpinBox->value() ;
resultLineEdit->setText ( QString::number ( result, 'f', decimals ) ) ;
numberLineEdit->setText ( QString::number ( input, 'f', decimals ) ) ;

}

void ConversionForm::init ()
{
numberLineEdit->setValidator ( new QDoubleValidator ( numberLineEdit ) ) ;
numberLineEdit->setText ( "10" ) ;
convert () ;
numberLineEdit->selectAll () ;
}


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
Old 05-23-2009, 07:06 AM   #12
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Header is for defining function prototypes and macro'es, not for storing source code. I wonder how you're getting object code from header actually.
 
Old 05-25-2011, 05:25 PM   #13
yaa see
LQ Newbie
 
Registered: May 2011
Posts: 1

Rep: Reputation: Disabled
sounds like a classical compiling without a main() function scenario. Try compile with -c flag.
 
Old 07-29-2011, 08:52 AM   #14
dunlopjp
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Rep: Reputation: 0
I noticed Dave Kelly, was pretty loud but no help
 
0 members found this post helpful.
Old 07-29-2011, 09:28 AM   #15
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Crap! yaa see and dunlopjp... you are fools for reopening a thread from two years ago.

Last edited by dwhitney67; 07-29-2011 at 09:30 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined reference error jacques83 Programming 20 04-11-2013 08:23 AM
Undefined reference error in MD5_Init() jacques83 Linux - Networking 0 11-11-2005 10:44 PM
undefined reference error Quest101 Programming 3 01-01-2005 01:27 PM
Undefined reference to function error Quest101 Linux - Newbie 0 12-30-2004 06:01 PM
emacs 77: undefined reference to `main' creznedmick Programming 2 09-16-2003 08:46 PM


All times are GMT -5. The time now is 01:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration