LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How To Compile Cpp Source And Header? (http://www.linuxquestions.org/questions/programming-9/how-to-compile-cpp-source-and-header-360468/)

kike_coello 09-05-2005 01:03 PM

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

vladmihaisima 09-05-2005 01:08 PM

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.

jayemef 09-05-2005 02:28 PM

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

kike_coello 09-06-2005 06:24 AM

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

deiussum 09-06-2005 10:28 AM

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

bigearsbilly 09-06-2005 10:39 AM

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}


kike_coello 09-07-2005 04:14 PM

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++

deiussum 09-07-2005 04:29 PM

Surely you've at least learned how to turn off CAPS lock by now, though... There's no need to yell at us. :cry:

kike_coello 09-07-2005 06:19 PM

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"

deiussum 09-07-2005 06:39 PM

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;
}


bigearsbilly 09-08-2005 03:22 AM

hmmm, yes.


All times are GMT -5. The time now is 04:00 PM.