user115897
user115897

Reputation:

nmake Makefile to Place object files in a separate directory

I have all my source files in the Source folder, The makefile is in the Makefile folder and I want all object files to be placed in Objects folder. It is a C project iam working on. All these three folders are in a folder named Project.

Project folder contains: Source Makefile Objects

iam trying to figure out a way to get the makefile to compile source from Source folder, i have tried using:

...
SOURCE_DIR=..\Source
OUTPUT_DIR=..\Objects
.c.obj:
    $(CC) $(SOURCE_DIR)\$*.c /Fo$(OUTPUT_DIR)\$*.obj
...  

but this is not working, i end up with an error message while says dont know how to make myfile.obj

Upvotes: 7

Views: 8815

Answers (4)

Sergei
Sergei

Reputation: 11

You should add target, changed the current directory to a directory where objective files you want to be made. The nmake sample Makefile file follows. In sample nmake compiles a.c b.c from current directory and places objs $(OBJ_FILES_DIR) directory and builds a SOME_STATIC.LIB in $(OUTDIR) directory.

# commmand to launch Makefile
nmake OBJ_FILES_DIR=my_obj_dir OUTDIR=my_lib_dir

# Makefile
STATICLIB = SOME_STATIC.LIB
OBJS = a.obj b.obj # objs in lib
O = $(OBJ_FILES_DIR)
S = $(MAKEDIR)  # remember source dir where a.c b.c reside and current    dir from which nmake launched.

.PHONY: $(STATICLIB)
.PHONY: CHDIR

$(STATICLIB): CHDIR $(OUTDIR)$(STATICLIB) # 

CHDIR:
    -md $(O) 
    cd $(O)

all: $(STATICLIB) 

$(OUTDIR)$(STATICLIB): $(OBJS)
    $(AR) $(ARFLAGS) -out:$@ $(OBJS)

{$(S)}.c.obj::
    $(CC) $(WFLAGS) $(CFLAGS) -c $<

Upvotes: 1

Tim
Tim

Reputation: 955

If you are using Visual Studio, sometimes you get the error message:

"don't know how to make '.obj'"

I was able to solve this by opening 'Makefile' and uncommenting out the lines underneath the comment that says "Uncomment these when compiling in Visual Studio 2010 to output a 32 bit binary". Find the comment that corresponds to your Visual Studio version and your architecture (32-bit/64-bit) and uncomment the lines underneath that!

Upvotes: 0

Hugo van der Sanden
Hugo van der Sanden

Reputation: 406

If it is literally saying don't know how to make myfile.obj, and not don't know how to make ..\Objects\myfile.obj then your problem is elsewhere - something in the makefile is asking for an object file in the current directory.

What is the rule that needs myfile.obj to build? I would guess that in that rule you are failing to specify $(OUTPUT_DIR)\myfile.obj as the dependency.

Upvotes: 0

Ron
Ron

Reputation: 61

Try this:

SOURCE_DIR=..\Source
OUTPUT_DIR=..\Objects
{$(SOURCE_DIR)}.c{$(OUTPUT_DIR)}.obj:
    $(CC) $* /Fo$<

As explained here: http://msdn.microsoft.com/en-us/library/hk9ztb8x.aspx

Upvotes: 6

Related Questions