我的工作目录如下:
./| ---- HelloWorld /| ---- | ---- main.cpp| ---- | ---- Makefile.am| ----宠物/| ---- | ---- Pet.h| ---- | ---- Pet.cpp| ---- build /| ---- configure.ac| ---- Makefile.am
我想使用自动工具来构建makefile,然后在构建目录中构建项目。
./ configure.ac是
# -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([Hello], [1.0], [[email protected]])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
AC_CONFIG_SRCDIR([HelloWorld/main.cpp])
AC_CONFIG_HEADERS([config.h])
# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
./ Makefile.am是
include HelloWorld/Makefile.am
请注意,我正在使用include来确保exe位于make命令运行的目录中。
../ HelloWorld / Makefile.am是
AM_CPPFLAGS=-I%D%/../Pet/ -I%D% -I%C%
#VPATH = ./HelloWorld ./Pet
bin_PROGRAMS=hello
hello_SOURCES=%D%/../Pet/Pet.h
hello_SOURCES+=%D%/../Pet/Pet.cpp
hello_SOURCES+=%D%/main.cpp
[如果有人想在自己的计算机上尝试,请在此处附加其他源代码:main.cpp
#include <stdio.h>
#include <vector>
#include "Pet.h"
int main() {
printf("Hello World\n");
std::vector<Pet*> all_pets;
Pet *dog = new Pet(string("Apple"));
all_pets.push_back(dog);
Pet *cat = new Pet(string("Pear"));
all_pets.push_back(cat);
for (int i = 0; i < all_pets.size(); i++) {
all_pets[i]->showName();
}
return 0;
}
**Pet.h**
#pragma once
#include <string>
using namespace std;
class Pet
{
public:
Pet(string name);
~Pet();
void showName();
void showIndex();
string _name;
int _index;
};
Pet.cpp
#include "Pet.h"
Pet::Pet(string name)
{
_name = name;
srand(2345);
_index = (rand() % 100);
}
Pet::~Pet()
{
}
void Pet::showIndex()
{
printf("Index is %d\n", _index);
}
void Pet::showName()
{
printf("Name is %s\n", _name.c_str());
}
问题陈述
./ $autoreconf --install
./ $./configure
./ $make
./build/ $../configure
./build/ $make
出现下图所示的错误:
我认为此错误是由编译器无法成功找到头文件引起的。我的第一个问题是为什么makefile.am中的AM_CPPFLAGS=-I%D%/../Pet/ -I%D% -I%C%
无法解决此问题?
因为编译器将在构建目录中创建.o文件,并使构建树的子目录布局与源树相同。因此,我可以通过将Pet.h文件复制到\ build \ Pet来解决此问题。但是,这意味着我总是需要将头文件复制到构建目录,这不方便。
我找到一些有关VPATH的信息。因此,我在./HelloWorld/Makefile.am中注释了#VPATH = ./HelloWorld ./Pet
。但是,这将给我带来一个新问题:
我的假设是VPATH设置与include makefile.am有所冲突。我的第二个问题是如何通过使用包含makefile正确使用VPATH?
我不小心通过将./HelloWorld/Makefile.am更改为]来解决问题>
AM_CPPFLAGS=-I%D%/../../Pet/ -I%D% -I%C% #VPATH = ../Pet #srcdir = @srcdir@ #VPATH = %D/Pet/ bin_PROGRAMS=hello hello_SOURCES=%D%/../../Pet/Pet.h hello_SOURCES+=%D%/../Pet/Pet.cpp hello_SOURCES+=%D%/main.cpp
注意,hello_SOURCES的路径已更改,并且标头路径与源路径不同。但是,为什么这可以解决问题?