使不更新可执行文件 [英] Make Does Not Update Executable

查看:72
本文介绍了使不更新可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下makefile:

CC=g++

all: happy

happy: happy.o HappyLetter.o
        $(CC) -o happy happy.o HappyLetter.o

happy.o: happy.cpp
        $(CC) -c happy.cpp

HappyLetter.o: HappyLetter.cpp
        $(CC) -c HappyLetter.cpp

clean:
        rm -rf *.o happy

,并且正在使用文件HappyLetter.cpp和happy.cpp(包括前者)创建一个名为happy的可执行文件.

我可以使用make成功地构建代码.但是,当我修改HappyLetter.cpp并再次键入'make'时,更改不会反映出来.仅当我键入"make clean"然后"make"时,它才有效.我希望发生的目标文件更新将回显到命令行:

$ make
g++ -c HappyLetter.cpp
g++ -o happy happy.o HappyLetter.o

但是,对HappyLetter.cpp的更新并没有反映在快乐中.

该问题在另一个方向上不起作用.也就是说,如果我修改happy.cpp,则在键入"make"后立即反映出更改.

我已经在Mac OS X和Ubuntu计算机上用三个make二进制文件复制了此问题.因此,我在编码中一定做错了.这是文件的文本,它们与makefile位于同一目录中:

happy.cpp

#include "HappyLetter.cpp"

int main()
{
  printf("Hello from happy.cpp!\n");
  HappyLetter *myObj = new HappyLetter();
  myObj->speak();
  return 0;
}

HappyLetter.cpp

#include <cstdio>

class HappyLetter {
  public:
    void speak()
    {
      printf("Hello from HappyLetter.cpp!\n");
    }
};

我认为问题很简单,但是我对检查的内容没有更多的想法.我有一个假设,即规则和依存关系的顺序无关紧要.

解决方案

我评论过:

首先,(通常)您不应该在happy.cpp中使用#include "HappyLetter.cpp"(即使这是可行的,但味道较差).您应该有一个单独的头文件(具有常规的包括卫队 )

#ifndef HAPPY_INCLUDED
//// file happy.h
#define HAPPY_INCLUDED 
  class HappyLetter {
 public:
   void speak();
 };
#endif /*HAPPY_INCLUDED*/

(您可以-或者可以-不决定在class HappyLetter之前在happy.h中使用#include <cstdio>;这两种方法都有很好的理由!)

然后,您应该拥有第一个源文件:

// file happy.cpp
 #include <cstdio>
 #include "happy.h"
int main() {
  printf("Hello from happy.cpp!\n");
  HappyLetter *myObj = new HappyLetter();
  myObj->speak();
  delete myObj;
  return 0;
}

顺便说一句,您应该使用智能指针

然后您有了第二个源文件:

 // file happyletter.cpp
 #include <cstdio>
 #include "happy.h"

 void HappyLetter::speak() {
  printf("Hello from HappyLetter.cpp!\n");
 }

最后,出现一个Makefile(请参见此处以获取灵感),例如:

 # file Makefile
 CXX= g++
 CXXFLAGS= -std=c++11 -Wall -Wextra -g
 RM= rm -f
 .PHONY: all clean

 all: happy-prog

 clean:
    $(RM) *.o *~ happy-prog

 happy-prog: happy.o happyletter.o

 happy.o: happy.cpp happy.h
 happyletter.o: happyletter.cpp happy.h

请注意对happy.h标头的明确依赖

我评论过,考虑使用 remake -xmake --trace进行调试您的Makefile.注意,GNU make有很多内置规则,请运行make -p来获取它们.

详细了解 C ++ 11 ,尤其是教程,很好的使用C ++编程本书,对标准有所了解(例如 n3337 草案).另请阅读有关 make 的信息,尤其是免费软件的源代码(请参阅 sourceforge github 等...找到一个).

(所以您同时得到了C ++源文件和Makefile错误!)

I have written the following makefile:

CC=g++

all: happy

happy: happy.o HappyLetter.o
        $(CC) -o happy happy.o HappyLetter.o

happy.o: happy.cpp
        $(CC) -c happy.cpp

HappyLetter.o: HappyLetter.cpp
        $(CC) -c HappyLetter.cpp

clean:
        rm -rf *.o happy

and am working with the files HappyLetter.cpp and happy.cpp (which includes the former) to create an executable named happy.

I can build the code successfully using make. However, when I modify HappyLetter.cpp and type 'make' again, the change is not reflected. It only works when I type 'make clean' and then 'make'. The update of the object file that I expect to take place is echoed to the command line:

$ make
g++ -c HappyLetter.cpp
g++ -o happy happy.o HappyLetter.o

However, the update to HappyLetter.cpp is not being reflected in happy.

The problem does not work in the other direction. That is, if I modify happy.cpp, the change is reflected immediately after I type 'make'.

I have replicated this problem with three make binaries on my Mac OS X, and also on an Ubuntu machine. So I must be doing something wrong in the coding. Here is the text of the files, which are in the same directory as the makefile:

happy.cpp

#include "HappyLetter.cpp"

int main()
{
  printf("Hello from happy.cpp!\n");
  HappyLetter *myObj = new HappyLetter();
  myObj->speak();
  return 0;
}

HappyLetter.cpp

#include <cstdio>

class HappyLetter {
  public:
    void speak()
    {
      printf("Hello from HappyLetter.cpp!\n");
    }
};

I believe the problem is something simple, but I have no more ideas about what to check. One assumption I have is that the ordering of the rules and dependencies does not matter.

解决方案

As I commented:

First, you should (conventionally) not #include "HappyLetter.cpp" in your happy.cpp (even if that is doable but poor taste). You should have a separate header file (with the conventional include guard)

#ifndef HAPPY_INCLUDED
//// file happy.h
#define HAPPY_INCLUDED 
  class HappyLetter {
 public:
   void speak();
 };
#endif /*HAPPY_INCLUDED*/

(You may -or not- decide to e.g. #include <cstdio> in your happy.h before the class HappyLetter; there are good reasons to do both ways!)

Then you should have a first source file:

// file happy.cpp
 #include <cstdio>
 #include "happy.h"
int main() {
  printf("Hello from happy.cpp!\n");
  HappyLetter *myObj = new HappyLetter();
  myObj->speak();
  delete myObj;
  return 0;
}

BTW, you should use smart pointers!

Then you have your second source file:

 // file happyletter.cpp
 #include <cstdio>
 #include "happy.h"

 void HappyLetter::speak() {
  printf("Hello from HappyLetter.cpp!\n");
 }

At last, a Makefile (see here for inspiration), like:

 # file Makefile
 CXX= g++
 CXXFLAGS= -std=c++11 -Wall -Wextra -g
 RM= rm -f
 .PHONY: all clean

 all: happy-prog

 clean:
    $(RM) *.o *~ happy-prog

 happy-prog: happy.o happyletter.o

 happy.o: happy.cpp happy.h
 happyletter.o: happyletter.cpp happy.h

Notice the explicit dependency on happy.h header

As I commented, consider using remake-x or make --trace to debug your Makefile. Notice that GNU make has a lot of built-in rules, run make -p to get them.

Read more about C++11, notably a tutorial, a good programming in C++ book, have a glance into the standard (e.g. n3337 draft). Read also about make, notably GNU make.

Study the source code of some existing free software coded in C++ (see sourceforge or github etc... to find one).

(so you got both your C++ source files and your Makefile wrong!)

这篇关于使不更新可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆