c++ class why need main?

后端 未结 5 2058
难免孤独
难免孤独 2021-01-27 01:28

Hello I\'m writing a little project in c++ where I would like to have some classes that does some work, I wrote the interfaces and the implementation of the classes.

The

5条回答
  •  别那么骄傲
    2021-01-27 01:39

    but I don't need (nor want) a main() in the class implementation.

    The function main is your entry-point. That is where execution begins. You need to have one and only one such function.

    But compiler give me "undefined reference to main" for animal.cpp, but I don't need a main there, or do I need it?

    Now, your problem looks like you have not linked the compiled forms of app.cpp and animal.cpp.

    I'm not so strong in Makefiles, I used something like g++ animal.h -o animal and g++ animal.cpp but it gives me the error above

    You don't compile headers. So don't use: g++ animal.h

    When you compiled the animal.cpp separately, g++ created an object file. You will also need to compile the app.cpp because you do need the main. Once you compile the app.cpp file you will have to link it with the animal object file created earlier. But if these did not get linked in, specially, the file containing the main function you will hit the error you are getting now.

    However, g++ takes care of what I have described above. Try something like this:

    g++ animal.cpp app.cpp -o test
    

    This will create an executable called test and you can run your application using:

    ./test
    

提交回复
热议问题