compiling h file doesn't always show errors

248 Views Asked by At

If I have an error on line 1, and I comment out the entirety of the H file, it doesn't always.. update?

It seems to be compiling a past version of the .h file, but if i intentionally put an error in the main.cpp file, then it realizes there are errors in the h file. Also it DOES sometimes show the errors that are just in the h file, but idk if it is after a certain period of time has elapsed

I would just try to put my code in a cpp file attached to the header, but the issue with that is the ugliest error i've ever seen and I'd rather it all stay in the header anyways since it'll only be like 15 lines of code.

Here's the makefile i'm using in case there is some weird thing in this causing the delay.. but I've had this issue just using raw "g++ *.h *.cpp" commands before, so that is probably not the issue. I've struggled with this issue for a long time now and had to put my last HW assignment all in one file because of it

MAINPROG=assignment01
CC=gcc
CXX=g++
CPPFLAGS=-g -std=c++11
LFLAGS=
CFLAGS=-g
TARGET=$(MAINPROG)
CPPS=$(wildcard *.cpp)
LINK=g++ $(CPPFLAGS)
OBJS=$(CPPS:%.cpp=%.o)

%.o: %.cpp
    $(CXX) $(CPPFLAGS) -MMD -o $@ -c $*.cpp


all: $(TARGET)  



$(TARGET): $(OBJS)
    $(LINK) $(FLAGS) -o $(TARGET) $^ $(LFLAGS)

clean:
    -/bin/rm -rf *.d *.o $(TARGET)
1

There are 1 best solutions below

0
Kingsley On

As πάντα ῥεῖ says, it's not normal to compile header files directly. They are incorporated into the compile when they are included into the cpp source.

Your makefile also does not link with the library stdc++ (libstdc++.a). I don't know if this is a problem when linking with g++, but it always is for me with gcc.

Oh, and rm -rf to cleanup! That's fairly aggressive, maybe just rm -f would be better, just in case someone accidentally puts / or .. as the target.

I think you should compile on the command line first, and then sort out the problems with your makefile. It might be worth posting copies of your code.

Generally I will compile simple code with:

gcc -g -Wall -o assignment01 assignment01.cpp -lstdc++

This gives: an exe named "assignment01", with debug info, all warnings, and links with c++ std libs.