-2

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
    Header files (`.h`) are usually not passed to the compiler directly, but referred to from translation units (`.cpp`) using `#include` statements. You can let the compiler automatically generate dependency lists for them (`-MMD`), which can be used in your makefile. Other than that, I have absolutely no clue what you're asking about. – πάντα ῥεῖ Sep 20 '18 at 00:01
  • @πάνταῥεῖ idk either, man. – critopadolf Sep 20 '18 at 00:05
  • Maybe [this](https://stackoverflow.com/questions/8025766/makefile-auto-dependency-generation) helps. – πάντα ῥεῖ Sep 20 '18 at 00:07

1 Answers1

0

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.

Kingsley
  • 14,398
  • 5
  • 31
  • 53