Skip to main content
GameDev.net gamedev.net
🔒 Locked

compile directories

Started by icecubeflower Jan 26, 2009 at 9:39 PM 7 replies 1k views
Original Post
icecubeflower
icecubeflower
I asked this a long time ago but it bugs me, isn't there a simple answer? Suppose you are coding two related projects and for both of them you wrote several .cpp files. Now suppose about half of the .cpp files in one project are exactly the same as the ones in the other project. In order to make sure both projects have the most up to date version of a .cpp file then every time you modify it in one project then you have to copy it to the other project and overwrite the old one. I HATE that! I mean isn't there some way to write those #include's with directory information or something? Or maybe in the "g++ whatever.cpp..." part specify it there somehow so I can have those common files in one directory and both projects grab them from the same place? Because I am like... in serious danger of heavily modifying a .cpp file in one project and forgetting to overwrite the old one in the other project. THEN slightly modifying the old .cpp file in the other project and REMEMBERING to overwrite the other one this time and losing a day's work. I'll commit suicide.
mutex
mutex
Refactor the shared code into a third project that is a library. Then have the original projects link to this reusable library.

Also, it sounds like you're not using version control. That'll significantly lessen the worry of losing files as long as you use it properly.

fcoelho
fcoelho
Create a library as mutex pointed, then, when compiling with g++ use the option -IPath/To/Includes to specify the include path for your library and use -LPath/To/Library when linking the library to your project
SiCrane
SiCrane
Quote:
Original post by icecubeflower
Or maybe in the "g++ whatever.cpp..." part specify it there somehow so I can have those common files in one directory and both projects grab them from the same place?

Yes. "g++ ../common/whatever.cpp" would go up one directory, then go into the common directory and process whatever.cpp. The exact syntax may be different depending on your operating system. Though it'd be easier to use an IDE to manage this for you.
icecubeflower
icecubeflower
I guess if I want to learn as little as possible I'll do it with SiCrane's "g++ ../common/whatever.cpp" method. That's what I was pretty much looking for.

Making a separate library sounds like a good idea though. I think.
Deylen
Deylen
Make files are actually really nice and easy once you get used to the syntax.

Basically what you should do is have a makefile in each directory with something like the following code:

##########################

PROJECT = main.c whatever.c draw.c
COMMON = ../common/whatever.c ../common/meh.c
LIBS = -lwhateverLibrary

ALL = $(PROJECT) $(COMMON)

main: $(ALL)
gcc $(ALL) -o main $(LIBS)

##########################

Using $(VARNAME) is like derefrencing a pointer, so the above code will include all the .c files listed in COMMON and in PROJECT.

You can also compile to .o files to really speed up compile times.
icecubeflower
icecubeflower
See that's what I thought, I thought I should be using a makefile but I asked on here where the best place to learn about makefiles was and everybody was like, "no, make is too hard, blah blah blah, use cmake or scons".

...so where's the best place to learn makefiles? I don't know anything about them.
icecubeflower
icecubeflower
Hey when you have all your code refactored so you can put part of it into a separate library, how do you do that? There's no main, you can't execute the library by itself.

Supppose you have main.cpp, file2.cpp, file3.cpp, file4.cpp.

Suppose file3.cpp, file3.h, file4.cpp, file4.h should all be compiled into a separate library. How do compile a library? What does it become when you compile it since we're not making an executable file?

Where do you put it in Linux? Should it go in the same place where the OpenGL and SDL libraries are?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.