LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 09-05-2005, 02:03 PM   #1
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Rep: Reputation: 17
Question How To Compile Cpp Source And Header?


HELLO I HAVE TROUBLE COMPILING 3 FILES WITH G++

FILES: project_1.cpp IntArray.h and IntArray.cpp

WHEN I COMPILED THIS FILES ON VISUAL C++ I GOT THE EXECUTABLE AND IT WORKED FINE BUT THEN WHEN I TRIED TO COMPILE IN UBUNTU IT WOULDN'T WORK

I KEPT GETTING A MESSAGE THAT SAID THAT THE HEADER FILE "INTARRAY.H" WASN'T FOUND OR SOMETHING, I'M TYPING FROM XP CAUSE I CAN'T GET THE FRIGGIN WIRELESS TO WORK ON UBUNTU, ANYWAYS, I'LL POST THE CODE IF YOU NEED IT

WHAT I THINK IS A MAKEFILE CAUSE I'M COMPILING EACH SEPARATELY, I ALSO TRIED :

1) g++ project_1.cpp IntArray.h IntArray.h
2) g++ -c project_1.cpp IntArray.h IntArray.h
3) g++ -cl project_1.cpp IntArray.h IntArray.h
4) g++ -l project_1.cpp IntArray.h IntArray.h

I ONLY GET THE OBJECT FILE FOR THE PROJECT_1.CPP FILE BUT I DON'T THINK THE REST OF THE FILES ARE COMPILING OR LINKING

CAN ANYBODY TELL ME HOW TO WRITE A MAKEFILE?

ALSO IF ANYBODY CAN RECOMMEND A GOOD GUI IDE FOR C++ OR JAVA
 
Old 09-05-2005, 02:08 PM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
First, if you write with Caps most people understand you are shouting. It is considered polite not to do this.

About you problem you could try:

g++ -I. project_1.cpp IntArray.cpp

-I. tells the compiler to look for .h files in the current directory.

For Java good IDE - Netbeans.
 
Old 09-05-2005, 03:28 PM   #3
jayemef
Member
 
Registered: Aug 2005
Location: Juniata College, PA
Distribution: Ubuntu, Slackware
Posts: 67

Rep: Reputation: 15
The compiler should already look for header files on its own if you properly #include[ed] it in the corresponding cpp file. You just need to specify all your cpp files during compilation. I would also highly recommend the -o option, as this allows you to name the output whatever you wish, which is much more helpful then a.out. So, for example, you would want to run
Code:
$ g++ -o Project_1 project_1.cpp IntArray.cpp
This will produce an executable called Project_1 that you can then execute with
Code:
$ ./Project_1

Last edited by jayemef; 09-05-2005 at 03:30 PM.
 
Old 09-06-2005, 07:24 AM   #4
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Original Poster
Rep: Reputation: 17
thanks guys

what the vladmihaisima dude said was correct, i looked up the man page for gcc and "-I ." indeed did make the compiler look in the specified folder for any header files. thanks again

still need help writing makefile, i don't know the syntax or how to begin. lol, newbie here
 
Old 09-06-2005, 11:28 AM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Also, filenames are case sensitive in Linux so INTARRAY.H is a different file from IntArray.h. So if your file is named IntArray.h, but you try to #include "INTARRAY.H" the compiler isn't going to find it. Make sure they match.

For a simple Makefile, try this: (Name it "Makefile", not "makefile" or "MAKEFILE")

Code:
# Create Project_1 if you execute just "make"
all: Project_1

#Create a rule for creating Project_1 executable
Project_1: Project_1.o IntArray.o
	g++ Project_1.o IntArray.o -o Project_1 
	
#Create rules for creating each intermediate object file
#Adding the header to the end tells make that if that file
#changes, the object needs to be recompiled
Project_1.o: Project_1.cpp IntArray.h
	g++ -c Project_1.c -o Project_1.o
	
IntArray.o: IntArray.cpp IntArray.h
	g++ -c IntArray.c -o IntArray.o
Note: There are other ways to create the above Makefile by substituting some of those names with symbols, or making a general rule to create .o files from .cpp, but I just created a nice simple one to try and help explain makefiles
 
Old 09-06-2005, 11:39 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
Try this:
Just change the TARGET field, should work and detect new files and changes.
Shouldn't need to touch it. (much)

(maybe )


relies on having makedepend though.



Code:
TARGET=blah_blah.blah
#	---------------------------------
#	Standard makefile template
#	just specify the target name above
#	and the make file should sort it
#	all out (hopefully)
#	---------------------------------

INCLUDEFILE = include.mk


# this should appear above the 'include' directive below
# calls for the target and then the cleanup
# =========================================
all: ${TARGET} cleanup 


-include ${INCLUDEFILE}

# ${INCLUDEFILE} should not exist when it's included.
# As it does not exist it is treated as a target.
# Therefore it is created anew each time,
# by the appropriate rule, below
# Which finds .cpp and .h files and calls makedepend for them
# =========================================================

${INCLUDEFILE}:
	@echo "### creating ${INCLUDEFILE}"
	@echo "### creating ${INCLUDEFILE}" > ${INCLUDEFILE}
	-@ echo  *.cpp|xargs echo SRCFILES = >> ${INCLUDEFILE}
	-@ echo  *.h|xargs echo HFILES = >> ${INCLUDEFILE}
	@  makedepend  -f${INCLUDEFILE} *.cpp


# (SRCFILES defined in INCLUDEFILE)
# =================================
OBJFILES = ${SRCFILES:.cpp=.o}


# this is the working target
# ==========================

${TARGET}: ${OBJFILES} 
	${CC} -o $@ ${OBJFILES}

help: print_help cleanup

print_help:
	echo  OBJFILES = ${OBJFILES}
	echo  SRCFILES = ${SRCFILES}
	cat ${INCLUDEFILE}
	

clean:cleanup
	-@ rm -f  *.o 

# we need to delete the INCLUDEFILE otherwise
# new source and header files won't be picked up
# next time
# =========
cleanup:
	@echo "### deleting ${INCLUDEFILE}"
	-@ rm -f  ${INCLUDEFILE}
 
Old 09-07-2005, 05:14 PM   #7
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Original Poster
Rep: Reputation: 17
DUDE

THAT WAS CONFUSING, YOU MUST THINK I KNOW WHAT I'M TALKING ABOUT, LOL, THANKS FOR THE LAST POST BUT I'M ON DATA STRUCTURES USING C++ IN COLLEGE, I'M NOT WORKING ON A BIG COMPANY PROJECT (YET), LOL, THANKS ANYWAYS, I MIGHT USE IT WHEN I TAKE ADVANCE C++
 
Old 09-07-2005, 05:29 PM   #8
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Surely you've at least learned how to turn off CAPS lock by now, though... There's no need to yell at us.
 
Old 09-07-2005, 07:19 PM   #9
kike_coello
Member
 
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88

Original Poster
Rep: Reputation: 17
Talking IS IT REALLY THAT ANNOYING?

HAHAHAHA, CUZ I THINK ITS STUPID TO THINK THAT BECAUSE SOMEBODY TYPES WITH THE CAPS LOCK ON PEOPLE ARE GONNA THINK THAT HE IS ACTUALLY SCREAMING, LOL, DUDE, THAT JUST SHOWS YOU SPEND TOO MUCH TIME IN CHAT ROOMS

DUDE I JUST WANTED TO DIFFERENTIATE BETWEEN WHAT I SAY AND MY CODE, NOW IF YOU ARE GONNA TRY TO GET TECHNICAL ON ME, LOL, AM A HAVE TO OPEN A CAN OF CYBERWHOOPASS, LOL, JK (I SUPPOSE YOU DO KNOW WHAT I MEAN BY LOL AND JK, LOL)

THANKS FOR THE HELP NOVAS, I GOT IT TO WORK, AND "SAY NO TO CHAT"
 
Old 09-07-2005, 07:39 PM   #10
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Well... it's an ettiquette that has been pretty well established since the early days of the Internet. I don't spend time in chat rooms, but I do on forums, and even before the web, I would read Usenet newsgroups. Even before that, I would read Fidonet boards from local BBSes. As long as I can remember, it's been considered shouting to use all caps. But hey, if you want to try and break years of tradition, be my guest. Just don't be surprised when you continue to get people who ask you to stop yelling at them.

If you want to differentiate your code, it would be much more differentiating to use [ code ] [ /code ] tags. (Remove the spaces and put your code between these two tags. Like so:

Code:
#include <iostream>

int main()
{
    std::cout << "This is how we do it" << std::endl;
 
    return 0;
}
 
Old 09-08-2005, 04:22 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
hmmm, yes.
 
  


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
How to compile a C++ file (.cpp) with allegro? RHLinuxGUY Programming 5 11-29-2005 03:06 AM
How do I compile a *.cpp Document in linux? njbrain Linux - Software 9 01-29-2004 08:43 PM
compile programs in c and cpp Manjula.S Linux - Software 2 10-15-2003 02:06 AM
Problems when trying to compile cpp files using qt headers siphiuel Linux - Software 2 08-10-2003 08:10 PM
to compile cpp on linux mandrake manpreet Programming 1 04-26-2001 01:45 AM


All times are GMT -5. The time now is 12:46 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration