LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-13-2011, 03:34 AM   #1
hjazz6
Member
 
Registered: Jan 2009
Posts: 36

Rep: Reputation: 0
Undefined reference to 'main' error in crt1.o function _start


Hi all,

I've having problems with my Makefile.

I'm trying to create a program from 2 files - main.cpp that contains the main function, and modules.c that contains the definitions of the functions that are called in main(). modules.c only contain function definitions, no main function.

My Makefile is as follows:

Code:
CC := gcc
CXX := g++
LINK := g++ -Wall
CFLAGS := -g 
CXXFLAGS := -g 
    
TARGET = program
    
$(TARGET): modules.o main.o
   $(LINK) -o $@ $< -lpcap
    
clean:
   rm *.o $(TARGET)
    
modules.o:
   $(CC) $(CFLAGS) -c modules.c -o $@ $<
    
main.o:
   $(CXX) $(CXXFLAGS) -c main.cpp -o $@ $<
I have included "modules.h", which contains all the function declarations, in my main.cpp.

When I try to make using this Makefile, I get the error

Code:
/usr/lib/gcc/x86_64-redhat-linux/4.4.4/../../../../lib64/crt1.o: In function '_start':
    (.text+0x20): undefined reference to 'main'
If I switch the order of modules.o and main.o in my $(TARGET) line, then I get errors that say "undefined reference to" the functions I have defined in modules.c, in main.cpp.

I don't know what is wrong.

Thank you.

Regards,
Rayne

Last edited by hjazz6; 04-13-2011 at 03:39 AM.
 
Old 04-13-2011, 06:19 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The issue is with the Makefile, when you attempt to link the object files. You are not obtaining both object files because you are using $<. You should have used $^.

There's some other minor issues with the Makefile; for example, -Wall is a compiler option, and thus is not applicable when linking. Also you should use LDFLAGS to specify the linker options, versus hard coding them when linking your program. You can also simplify your rules with regards to compiling your source files, or omit them altogether. Note that "make" has built-in rules that oftentimes suffice for most projects; thus there is no need to redefine the rules.

Here's a simple Makefile that will satisfy your goals:
Code:
TARGET   = program

CFLAGS   = -g -Wall
CXXFLAGS = -g -Wall
LDFLAGS  = -lpcap


$(TARGET) : modules.o main.o
        $(CXX) -o $@ $^ $(LDFLAGS)

clean :
        $(RM) *.o $(TARGET)

P.S. I never specify a wildcard string when removing files; a simple typo can cost you a lot, especially if you remove special files. Consider the following:

Code:
TARGET   = program

C_SRCS   = modules.c
CXX_SRCS = main.cpp

OBJS     = $(C_SRCS:.c=.o)
OBJS    += $(CXX_SRCS:.cpp=.o)

CFLAGS   = -g -Wall
CXXFLAGS = -g -Wall
LDFLAGS  = -lpcap


$(TARGET) : $(OBJS)
        $(CXX) -o $@ $^ $(LDFLAGS)

clean :
        $(RM) $(OBJS) $(TARGET)
 
1 members found this post helpful.
Old 07-28-2011, 01:53 PM   #3
dunlopjp
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Rep: Reputation: 0
I'm getting a similar error when attempting to build OpenFOAM 1.7.x.

14:41:39$ g++ -DlinuxIA64 -DWM_DP -Wextra -Wno-unused-parameter -Wold-style-cast -O3 -DNoRepository -ftemplate-depth-40 -IlnInclude -I. -I/app2/openfoam/platforms/linux.altix/OpenFOAM-1.7.x/src/OpenFOAM/lnInclude -I/app2/openfoam/platforms/linux.altix/OpenFOAM-1.7.x/src/OSspecific/POSIX/lnInclude -fPIC -Xlinker --add-needed Make/linuxIA64GccDPOpt/ansysToFoam.o -L/app2/openfoam/platforms/linux.altix/OpenFOAM-1.7.x/lib/linuxIA64GccDPOpt -lOpenFOAM -ldl -lm -o /app2/openfoam/platforms/linux.altix/OpenFOAM-1.7.x/applications/bin/linuxIA64GccDPOpt/ansysToFoam
/usr/lib/crt1.o: In function `_start':
init.c.text+0x41): undefined reference to `main'
collect2: ld returned 1 exit status
14:41:52$

And another attempt with the -Wall argument

14:49:50$ g++ -DlinuxIA64 -DWM_DP -Wall -Wextra -Wno-unused-parameter -Wold-style-cast -O3 -DNoRepository -ftemplate-depth-40 -IlnInclude -I. -I/app2/openfoam/platforms/linux.altix/OpenFOAM-1.7.x/src/OpenFOAM/lnInclude -I/app2/openfoam/platforms/linux.altix/OpenFOAM-1.7.x/src/OSspecific/POSIX/lnInclude -fPIC -Xlinker --add-needed Make/linuxIA64GccDPOpt/ansysToFoam.o -L/app2/openfoam/platforms/linux.altix/OpenFOAM-1.7.x/lib/linuxIA64GccDPOpt -lOpenFOAM -ldl -lm -o /app2/openfoam/platforms/linux.altix/OpenFOAM-1.7.x/applications/bin/linuxIA64GccDPOpt/ansysToFoam
/usr/lib/crt1.o: In function `_start':
init.c.text+0x41): undefined reference to `main'
collect2: ld returned 1 exit status
14:51:05$

Any assistance would be greatly appreciated.

Thanks
Jim Dunlop
 
Old 07-30-2011, 06:51 PM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Post #3, @dunlopjp

Which Linux OS are you using ? Like Fedora 15, CentOS 6, etc.

And "g++ -DlinuxIA64" looks like you are compiling for ia64.
Is that correct ?
 
Old 08-01-2011, 07:06 AM   #5
dunlopjp
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Rep: Reputation: 0
08:04:11$ uname -a
Linux hawk-0 2.6.16.60-0.66.1-default #1 SMP Fri May 28 12:10:21 UTC 2010 ia64 ia64 ia64 GNU/Linux

Yes it's ia64

The distribution is SuSE
 
  


Reply



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
Getting undefined reference to main error?? Mistro116@yahoo.com Programming 14 07-29-2011 08:28 AM
Undefined reference to main error in winavr gcc plugin in avr studio zack670303 Programming 1 02-26-2009 12:52 PM
executable file: Which path will go from _start to main function valpa Programming 1 09-04-2008 08:08 PM
undefined reference to main cjbrockwell Programming 3 09-06-2006 05:09 PM
Undefined reference to function error Quest101 Linux - Newbie 0 12-30-2004 05:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration