The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
(HOMEWORK) undefined reference to main ANSI C
Discuss (HOMEWORK) undefined reference to main ANSI C in the C Programming forum on Dev Shed. (HOMEWORK) undefined reference to main ANSI C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 2nd, 2013, 11:51 PM
|
 |
Contributing User
|
|
|
|
|
(HOMEWORK) undefined reference to main ANSI C
So I've been coding this in C99 for a homework assignment only to reread through the requirements and find out that I have to compile in ANSI C instead. I'm not familiar with all of the differences between ANSI C and C99, other than declarations and code mixing not being allowed in ANSI C. So I made sure to separate those out and tried compiling with "gcc prog1.c -lm -o prog1" as opposed to "gcc prog1.c -std=c99". With the latter, my code compiles and runs without any noticeable issues. With the former, I get the following error which I'm having a hard time understanding:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
Code:
// ---------------------------------------------------------------------------
// NAME: Anthony Wolf User ID: xxxx
// DUE DATE: 02/06/2013
// PROGRAM ASSIGNMENT #1
// FILE NAME: prog1.c
// PROGRAM PURPOSE:
// Create three child processes to each run different tasks.
// Child 1 heapsorts a random array of integers
// Child 2 computes a fibonacci number
// Child 3 computes a probability for Buffon's Needle
// ---------------------------------------------------------------------------
# define _XOPEN_SOURCE
# define PI 3.141592
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <math.h>
int fibonacci(int n)
{
if(n == 1 || n == 2)
return 1;
else if(n <= 0)
return 0;
else
return fibonacci(n-2) + fibonacci(n-1);
}
void heapInsert(int insertMe, int* array, int* size)
{
int i;
*size += 1;
i = *size - 1;
while(i > 0 && array[i/2] > insertMe) //while parent greater than insertMe
{
array[i] = array[i/2];
i = i/2; //traverse to parent
}
array[i] = insertMe;
}
void heapDown(int* array, int index, int size)
{
int l = 2*index+1;
int r = 2*index+2;
int leastIndex, temp;
if(l < size && array[l] < array[index])
leastIndex = l;
else
leastIndex = index;
if(r < size && array[r] < array[leastIndex])
leastIndex = r;
if(leastIndex != index)
{
//swap
temp = array[index];
array[index] = array[leastIndex];
array[leastIndex] = temp;
heapDown(array, leastIndex, size);
}
}
int heapRemoveMin(int* array, int* size)
{
int oldRoot = array[0];
int i = 0;
int temp;
//Store the old root r of the tree into a temporary variable, and replace the root node with the last element in the heap
assert(*size > 0);
array[0] = array[*size-1];
*size -= 1;
heapDown(array, 0, *size);
//heap down
// [0]
// [1] [2]
// [3] [4] [5] [6]
return oldRoot;
}
/******
input integers m, n, and r
/******/
int main(int argc, char* argv[])
{
pid_t pid1, pid2, pid3;
int i;
double d, a, x;
int t = 0;
int randomArray[atoi(argv[1])];
int sortedArray[atoi(argv[1])];
int size = 0;
//siginfo_t infop;
if(argc != 4)
{
fprintf(stderr, "expected 3 arguments\n");
exit(EXIT_FAILURE);
}
pid1 = fork();
if(pid1 == -1)
exit(EXIT_FAILURE); //failure
else if(pid1 != 0) //parent
{
pid2 = fork();
if(pid2 == -1)
exit(EXIT_FAILURE); //failure
else if(pid2 != 0) //still parent
{
pid3 = fork();
if(pid3 == -1)
exit(EXIT_FAILURE); //failure
else if(pid3 != 0) //still parent, done forking
{
//3 child processes have been forked, wait for them
waitid(P_PID, pid1, 0, WEXITED);
printf("p1 exited\n");
waitid(P_PID, pid2, 0, WEXITED);
printf("p2 exited\n");
waitid(P_PID, pid3, 0, WEXITED);
printf("p3 exited\n");
exit(EXIT_SUCCESS);
}
else //third forked process
{
srand(time(NULL));
for(i = 0; i < atoi(argv[3]); i += 1)
{
/*
This will generate a number from 0.0 to some arbitrary float, X:
float r2 = (float)rand()/((float)RAND_MAX/X);
*/
d = (float)rand()/((float)RAND_MAX/.999999);
a = (float)rand()/((float)RAND_MAX/(2*PI-.000001));
x = d + sin(a);
if(x < 0 || x > 1)
t += 1;
}
printf("P3: Estimated Probability is %f\n", ((double)t)/atof(argv[3]));
printf("p3 finished!\n");
//sleep(4);
//printf("p3 done sleeping\n");
exit(EXIT_SUCCESS);
}
}
else //second forked process
{
printf("P2: The %dth Fibonnaci number is %d\n", atoi(argv[2]), fibonacci(atoi(argv[2])));
printf("p2 finished!\n");
//sleep(4);
//printf("p2 done sleeping\n");
exit(EXIT_SUCCESS);
}
}
else //first forked process
{
/***
"The first process sorts m random integers using the heap sort...
Prints the generated random integers
Uses the heap sort to sort this array
Prints the sorted array"
***/
srand(time(NULL));
for(i = 0; i < atoi(argv[1]); i += 1) //generate random array
randomArray[i] = rand()%100;
for(i = 0; i < atoi(argv[1]); i += 1)
{
assert(size <= atoi(argv[1]) && size >= 0);
printf("P1: Random element %d: %d\n", i, randomArray[i]);
heapInsert(randomArray[i], sortedArray, &size);
}
//for(int i = 0; i < atoi(argv[1]); i += 1)
// printf("p1: heap[%d] = %d\n", i, sortedArray[i]);
printf("P1: *** Sorted Elements ***\n");
for(i = 0; i < atoi(argv[1]); i += 1)
{
assert(size <= atoi(argv[1]));
assert(size >= 0);
printf("P1: sorted[%d] = %d\n", i, heapRemoveMin(sortedArray, &size));
}
printf("p1 finished!\n");
//sleep(4);
//printf("p1 done sleeping\n");
exit(EXIT_SUCCESS);
}
}
I did try googling this error but I didn't find anything particularly useful about things which are allowed in C99 but not ANSI C, so then I tried googling that too but so far I'm not finding anything which seems relevant. Is my problem what I think it is, or is it something else entirely?
Last edited by jakotheshadows : February 2nd, 2013 at 11:59 PM.
|

February 3rd, 2013, 12:24 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
The Wikipedia article on C99, http://en.wikipedia.org/wiki/C99, lists the new stuff in C99, which should clue you in what's not allowed in ANSI C. In particular, there's:
Quote: | intermingled declarations and code: variable declaration is no longer restricted to file scope or the start of a compound statement (block), similar to C++ |
That means that in ANSI C you have to place all declarations before the first executable code.
I think that _start is the program's start-up code whose job, in part, is to call main. I think that it's complaining that it cannot find main. Consider this part of your code:
Code:
/******
input integers m, n, and r
/******/
int main(int argc, char* argv[])
{
I don't know about nested comments, but do you think that maybe that's what's happened? That that second start-comment slash might have caused main to be commented out? Try removing it (the one in red) and see what happens.
|

February 3rd, 2013, 12:44 AM
|
 |
Contributing User
|
|
|
|
Code:
/****** input integers m, n, and r ******/
Changed that comment, still same error when trying to compile.
|

February 3rd, 2013, 01:38 AM
|
 |
Contributing User
|
|
|
|
Oops. I wasn't typing what I thought I was typing. One of those days problem solved! 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|