“undefined reference to” in G++ Cpp

眉间皱痕 提交于 2019-11-26 13:52:23

g++ main.cpp Help.cpp

You have to tell the compiler all the files that you want it to compile, not just the first one.

You should add help.o to your g++ line:

g++ -c help.cpp -o help.o
g++ help.o main.cpp

By splitting it to two lines you can save compilation time (in case of larger projects), because you can compile help.cpp only when it was changed. make and Makefile used well will save you a lot of headache:

#Makefile
all: main

main: help main.cpp
    g++ -o main help.o main.cpp

help: help.cpp
    g++ -c -o help.o help.cpp

I had the same problem with my Linux Lubuntu distro and it was creating the problem for my constructor, destructor, it is not recognizing them.

Actually, this goes off if you just compile all of the three files together. So, once you saved all your files, just do this:

$ g++ main.cpp Help.h Help.cpp
$ ./a.out

./a.out is the executable file for the Linux,Sorry but I don't know about the Windows. And your program would run smoothly.

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