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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-05-2005, 01:03 PM
|
#1
|
|
Member
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88
Rep:
|
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
|
|
|
|
09-05-2005, 01:08 PM
|
#2
|
|
Member
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196
Rep:
|
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.
|
|
|
|
09-05-2005, 02:28 PM
|
#3
|
|
Member
Registered: Aug 2005
Location: Juniata College, PA
Distribution: Ubuntu, Slackware
Posts: 67
Rep:
|
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
Last edited by jayemef; 09-05-2005 at 02:30 PM.
|
|
|
|
09-06-2005, 06:24 AM
|
#4
|
|
Member
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88
Original Poster
Rep:
|
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
|
|
|
|
09-06-2005, 10:28 AM
|
#5
|
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
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
|
|
|
|
09-06-2005, 10:39 AM
|
#6
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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}
|
|
|
|
09-07-2005, 04:14 PM
|
#7
|
|
Member
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88
Original Poster
Rep:
|
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++
|
|
|
|
09-07-2005, 04:29 PM
|
#8
|
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
Surely you've at least learned how to turn off CAPS lock by now, though... There's no need to yell at us.
|
|
|
|
09-07-2005, 06:19 PM
|
#9
|
|
Member
Registered: Jul 2005
Location: maryland
Distribution: Ubuntu 9.04
Posts: 88
Original Poster
Rep:
|
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"
|
|
|
|
09-07-2005, 06:39 PM
|
#10
|
|
Member
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895
Rep:
|
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;
}
|
|
|
|
09-08-2005, 03:22 AM
|
#11
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
hmmm, yes.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
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
|
|
|
All times are GMT -5. The time now is 01:10 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|