Why do I have to double run make to fully compile my program? GNU c++

断了今生、忘了曾经 提交于 2019-12-12 00:27:26

问题


Im terrible with understanding what goes on behind the scenes when running programs.

On my schools server, I just use gcc and pretty much the same code every time I need to make a makefile lol.

I downloaded my program to debug it in NetBeans and after hours/days.. I finally ALMOST have everything working.

After looking at a few posts on here and other sites, I saw that for some reason I need to use g++ instead of gcc to compile since I'm using a MacBook. Which I don't really understand.. But if I change gcc to g++ and run this line in my makefile:

Edit:

 g++ -c $< -o $@ -std=c++0x -lstdc++

I get an error. But if I remove "-std=c++0x" and run make again.. Everything is good to go.

But if I run my "make clean" I have to do it over again..

Will this mess everything up when I put the program back on my schools server? Or should it be fine as long as I have a makefile on the server that is different from the makefile on my MacBook?

Can someone help expelling why this is happening and how I could possibly fix it?

Here's my makefile the first time I run make:

OBJECTS = Ammunition.o Armor.o Consumable.o 
HEADERS = Ammunition.h Armor.h Consumable.h 

all: Jhack

%.o: %.cpp $(HEADERS)
    g++ -c $< -o $@ -std=c++0x

Jhack: $(OBJECTS) main.o
    g++ -o Jhack $^

clean:
    rm -f *.o Jhack

run: Jhack
    ./Jhack

Thanks.


回答1:


First, for Linux GNU C compilers, gcc is a C compiler whereas g++ is a C++ compiler. Which means when you compile with gcc, all your error messages are basically saying, "I don't know what all this strange syntax is."

Your school isn't using a Linux server, or "gcc" there may be a link to a c++ compiler. Or the core make rules are replacing your compiler choice with g++.

I don't think there's all that much you need to worry about in this regard. Just remember the difference, and possibly replace gcc and g++ with a macro in your makefile.

As for the -std option, that's choosing which version of the C++ standard you want to compile against. Since some of your code requires 2011 C++ standard, you need to specify that with your -std option. If you look at your g++ man pages, it should tell you what options are supported.

You can try -std=c++0x or -std=c++11 or -std=gnu++11

Any of those should give you the functionality you require. Based on what I've searched for, you don't need to specify -std=stdc++ since i think that's included by g++ by default.




回答2:


You're probably usign an old, experimental version of GCC. "ConceptGCC" from the comments, and the -std=c++0x flag tell me so. Modern GCC variants would use -std=c++11 instead.

You never need to use g++ instead of gcc. It's just a convenience, so you don't need to pass all gcc options required to compile C++. In particular, -lstdc++ is implied by g++.

Since you're using a rather unusual "ConceptGCC" package, I'd suggest you replace it by the normal GCC version 4.8.0.

To debug what make is trying to do, run make -n. You can then run these commands manually to see precisely what happens.




回答3:


Justin, I don't know what std=c++0x does (I looked it up but I don't know what "experimental features" are possibly being affected...), but if removing it helps, then just remove it.

What you have described sounds to me like some objects require the std=c++0x flag and some won't build with it. Would you try just removing the flag, cleaning and then rebuilding?

Worst case - remove the variable rule and write three different rules for each of your object files.



来源:https://stackoverflow.com/questions/16272046/why-do-i-have-to-double-run-make-to-fully-compile-my-program-gnu-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!