Linking files in g++

后端 未结 4 1285
甜味超标
甜味超标 2020-12-01 11:17

Recently I have tried to compile a program in g++ (on Ubuntu). Usually i use Dev-C++ (on Windows) and it works fine there as long as I make a project and put all the necessa

4条回答
  •  执笔经年
    2020-12-01 11:50

    You can also check for correct #include tags within filename.cpp. Assume that filename.cpp uses code contained in myclass.h present in the same directory as filename.cpp. Assume that the class that g++ says is undefined is contained in myclass.h and defined in myclass.cpp. So, to correctly include myclass.h within filename.cpp, do the following:

    1. In filename.cpp:
    #include   
    #include   
    //..source code.  
    
    1. In the makefile:
    filename.o: myclass.C myclass.h filename.cpp
    g++ -I./ -c filename.cpp -o filename.o
    
    myclass.o: myclass.C myclass.h  
    g++ -c myclass.C -o myclass.o
    

    In the above, note the use of -I. option when compiling filename.cpp. The -I asks g++ to include the path following the -I part into the search path. That way myclass.h is correctly included.

    In the absence of more information (the source maybe), it is difficult to say with any accuracy where the problem lies. All attempts will be but stabs in the dark.

提交回复
热议问题