5

part of my makefile looks like this ...

ifdef vis 
flg += -framework GLUT -framework OpenGL -Dvis
obj += camfun.o glfuns.o
endif

...

all: driver.cpp header.h $(obj) 
  $(cc) -o $(exe) driver.cpp $(obj) $(flg) $(lib)

funs.o: header.h funs.cpp
  $(cc) -c funs.cpp $(flg)

glfuns.o: header.h glfuns.cpp
  $(cc) -c glfuns.cpp $(flg)

camfun.o: header.h camfun.cpp
  $(cc) -c camfun.cpp $(flg)

which gives me the following warning upon compilation:

g++ -c camfun.cpp -Wno-write-strings -O2 -framework GLUT -framework OpenGL -Dvis
i686-apple-darwin10-g++-4.2.1: -framework: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: GLUT: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -framework: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: OpenGL: linker input file unused because linking not done

now, i know i am getting that warning because i definitely should (the -c option specifically tells it not to link)! but i want to turn it off, i know i am linking too much and i'm ok with that. how do i turn off that warning?

drjrm3
  • 4,474
  • 10
  • 53
  • 91

2 Answers2

6

You can't turn off the warning, except by not passing those flags. It's not that you're linking "too much", it's that by giving -c you're not linking at all. Any flags that are supposed to be passed to the linker will show a warning if you give them to a command that doesn't run the linker. If you don't want those warnings, then don't pass linker flags to your compiler. Separate out your flags in your makefile into two different variables: one set that goes to both compiler and linker (often people use CFLAGS for this but it's just a convention) and one set that goes just to the linker (often LDFLAGS).

Also you shouldn't link in the all target: have a separate target for the executable so that if you run make twice in a row without changes, the second invocation will do nothing.

And in POSIX, command options should generally come before arguments not afterwards.

And of course, using pattern rules helps avoid redundancy.

For example:

ifdef vis 
ccflg += -Dvis
ldflg += -framework GLUT -framework OpenGL
obj += camfun.o glfuns.o
endif

...

all: $(exe)

$(exe): driver.cpp header.h $(obj) 
        $(cc) -o $(exe) $(ccflg) $(ldflg) driver.cpp $(obj) $(lib)

%.o: %.cpp header.h
        $(cc) -c $(ccflg) $<
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • when i split them into `$(ldflg)` and `$(ccflg)`, i get a bunch of warnings after `g++ -c glfuns.cpp -Wno-write-strings -O2 -Dvis` saying that certain things (GLUT_DOUBLE, for example) were not declared in this scope. when i compile that line with ldflg and ccflg, though, it compiles but i get those warnings. EDIT: i had ccflg and ldflg mixed up! thanks! – drjrm3 Mar 16 '12 at 13:09
  • I have a similar problem: with passing -Wl,-rpath-link to gcc. It's part of our CFLAGS, and would take a bit of re-work to get rid of. I know it's wrong, but I was hoping for a quick solution. Blast modern GCC for wanting things to be correct! (that's sarcasm BTW) – David Dombrowsky Sep 11 '12 at 22:07
0

-c means that you * aren't linking *. So don't pass -framework.

bmargulies
  • 97,814
  • 39
  • 186
  • 310