我为这类问题感到非常抱歉,我不久前就开始学习编码,通常无法轻松地发现容易的错误。
错误如下,也包含文件。我也想说,如果我将book.h的内容复制并粘贴到Bdynamic_array.h,请对book.cpp做同样的操作,并删除#include book.h行,我的程序可以使用“ make”进行编译。而且工作也很好。
错误
CMakeFiles/a.out.dir/Bdynamic_array.cpp.o: In function `BDynamicArray::shitleft(int, int)':
Bdynamic_array.cpp:(.text+0x381): undefined reference to `Book::operator=(Book&)'
CMakeFiles/a.out.dir/Bdynamic_array.cpp.o: In function `BDynamicArray::shitright(int, int)':
Bdynamic_array.cpp:(.text+0x456): undefined reference to `Book::operator=(Book&)'
CMakeFiles/a.out.dir/Bdynamic_array.cpp.o: In function `Book* std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m<Book*, Book*>(Book*, Book*, Book*)':
Bdynamic_array.cpp:(.text._ZNSt11__copy_moveILb0ELb0ESt26random_access_iterator_tagE8__copy_mIP4BookS4_EET0_T_S6_S5_[_ZNSt11__copy_moveILb0ELb0ESt26random_access_iterator_tagE8__copy_mIP4BookS4_EET0_T_S6_S5_]+0x40): undefined reference to `Book::operator=(Book&)'
CMakeFiles/a.out.dir/Blist.cpp.o: In function `BList::insert(int, Book)':
Blist.cpp:(.text+0x182): undefined reference to `Book::operator=(Book&)'
CMakeFiles/a.out.dir/Blist.cpp.o: In function `BList::add(Book)':
Blist.cpp:(.text+0x296): undefined reference to `Book::operator=(Book&)'
CMakeFiles/a.out.dir/Blist.cpp.o: In function `BList::listOut()':
Blist.cpp:(.text+0x34f): undefined reference to `operator<<(std::ostream&, Book const&)'
包含BDynamic_array.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "book.h"
#include "Bdynamic_array.h"
using namespace std;
包括BDynamic_array.h
#include <iostream>
#include <string>
#include <sstream>
#include "book.h"
using namespace std;
包含Blist.h
#include "Bdynamic_array.h"
包含Blist.cpp
#include <iostream>
#include "Bdynamic_array.h"
#include "Blist.h"
Book.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "book.h"
using namespace std;
ostream &operator<< (ostream &stream, const Book &book)
{
stream << book.id << ": " << book.title << ", pages - " << book.pages << ", illustrstions - " << book.illustrations << ", number of characters - " << book.numberofCharacters;
return stream;
}
Book& Book::operator=(Book &other)
{
id = other.id;
title = other.title;
pages = other.pages;
illustrations = other.illustrations;
numberofCharacters = other.numberofCharacters;
return other;
}
Book.h
#ifndef ____BBOOK__HJKIPU13
#define ____BBOOK__HJKIPU13
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct Book {
size_t id;
string title;
size_t pages;
bool illustrations;
size_t numberofCharacters;
Book& operator=(Book& other);
//ostream &operator<< (ostream &stream);
};
ostream &operator<< (ostream &stream, const Book &book);
#endif
makefile
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.10
# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:
#=============================================================================
# Special targets provided by cmake.
# Disable implicit rules so canonical targets will work.
.SUFFIXES:
# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =
.SUFFIXES: .hpux_make_needs_suffix_list
# Suppress display of executed commands.
$(VERBOSE).SILENT:
# A target that is always out of date.
cmake_force:
.PHONY : cmake_force
#=============================================================================
# Set environment variables for the build.
# The shell in which to execute make rules.
SHELL = /bin/sh
# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake
# The command to remove a file.
RM = /usr/bin/cmake -E remove -f
# Escaping for special characters.
EQUALS = =
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/igor/progbase2/labs/lab2
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/igor/progbase2/labs/lab2/build
#=============================================================================
# Targets provided globally by CMake.
# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache
# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast
# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..."
/usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available.
.PHONY : edit_cache
# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/igor/progbase2/labs/lab2/build/CMakeFiles /home/igor/progbase2/labs/lab2/build/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/igor/progbase2/labs/lab2/build/CMakeFiles 0
.PHONY : all
# The main clean target
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean
# The main clean target
clean/fast: clean
.PHONY : clean/fast
# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast
# clear depends
depend:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend
#=============================================================================
# Target rules for targets named a.out
# Build rule for target.
a.out: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 a.out
.PHONY : a.out
# fast build rule for target.
a.out/fast:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/build
.PHONY : a.out/fast
Bdynamic_array.o: Bdynamic_array.cpp.o
.PHONY : Bdynamic_array.o
# target to build an object file
Bdynamic_array.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/Bdynamic_array.cpp.o
.PHONY : Bdynamic_array.cpp.o
Bdynamic_array.i: Bdynamic_array.cpp.i
.PHONY : Bdynamic_array.i
# target to preprocess a source file
Bdynamic_array.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/Bdynamic_array.cpp.i
.PHONY : Bdynamic_array.cpp.i
Bdynamic_array.s: Bdynamic_array.cpp.s
.PHONY : Bdynamic_array.s
# target to generate assembly for a file
Bdynamic_array.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/Bdynamic_array.cpp.s
.PHONY : Bdynamic_array.cpp.s
Blist.o: Blist.cpp.o
.PHONY : Blist.o
# target to build an object file
Blist.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/Blist.cpp.o
.PHONY : Blist.cpp.o
Blist.i: Blist.cpp.i
.PHONY : Blist.i
# target to preprocess a source file
Blist.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/Blist.cpp.i
.PHONY : Blist.cpp.i
Blist.s: Blist.cpp.s
.PHONY : Blist.s
# target to generate assembly for a file
Blist.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/Blist.cpp.s
.PHONY : Blist.cpp.s
csv.o: csv.cpp.o
.PHONY : csv.o
# target to build an object file
csv.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/csv.cpp.o
.PHONY : csv.cpp.o
csv.i: csv.cpp.i
.PHONY : csv.i
# target to preprocess a source file
csv.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/csv.cpp.i
.PHONY : csv.cpp.i
csv.s: csv.cpp.s
.PHONY : csv.s
# target to generate assembly for a file
csv.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/csv.cpp.s
.PHONY : csv.cpp.s
dynamic_array.o: dynamic_array.cpp.o
.PHONY : dynamic_array.o
# target to build an object file
dynamic_array.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/dynamic_array.cpp.o
.PHONY : dynamic_array.cpp.o
dynamic_array.i: dynamic_array.cpp.i
.PHONY : dynamic_array.i
# target to preprocess a source file
dynamic_array.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/dynamic_array.cpp.i
.PHONY : dynamic_array.cpp.i
dynamic_array.s: dynamic_array.cpp.s
.PHONY : dynamic_array.s
# target to generate assembly for a file
dynamic_array.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/dynamic_array.cpp.s
.PHONY : dynamic_array.cpp.s
dynamic_array2.o: dynamic_array2.cpp.o
.PHONY : dynamic_array2.o
# target to build an object file
dynamic_array2.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/dynamic_array2.cpp.o
.PHONY : dynamic_array2.cpp.o
dynamic_array2.i: dynamic_array2.cpp.i
.PHONY : dynamic_array2.i
# target to preprocess a source file
dynamic_array2.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/dynamic_array2.cpp.i
.PHONY : dynamic_array2.cpp.i
dynamic_array2.s: dynamic_array2.cpp.s
.PHONY : dynamic_array2.s
# target to generate assembly for a file
dynamic_array2.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/dynamic_array2.cpp.s
.PHONY : dynamic_array2.cpp.s
fs.o: fs.cpp.o
.PHONY : fs.o
# target to build an object file
fs.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/fs.cpp.o
.PHONY : fs.cpp.o
fs.i: fs.cpp.i
.PHONY : fs.i
# target to preprocess a source file
fs.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/fs.cpp.i
.PHONY : fs.cpp.i
fs.s: fs.cpp.s
.PHONY : fs.s
# target to generate assembly for a file
fs.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/fs.cpp.s
.PHONY : fs.cpp.s
list.o: list.cpp.o
.PHONY : list.o
# target to build an object file
list.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/list.cpp.o
.PHONY : list.cpp.o
list.i: list.cpp.i
.PHONY : list.i
# target to preprocess a source file
list.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/list.cpp.i
.PHONY : list.cpp.i
list.s: list.cpp.s
.PHONY : list.s
# target to generate assembly for a file
list.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/list.cpp.s
.PHONY : list.cpp.s
list2.o: list2.cpp.o
.PHONY : list2.o
# target to build an object file
list2.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/list2.cpp.o
.PHONY : list2.cpp.o
list2.i: list2.cpp.i
.PHONY : list2.i
# target to preprocess a source file
list2.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/list2.cpp.i
.PHONY : list2.cpp.i
list2.s: list2.cpp.s
.PHONY : list2.s
# target to generate assembly for a file
list2.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/list2.cpp.s
.PHONY : list2.cpp.s
main.o: main.cpp.o
.PHONY : main.o
# target to build an object file
main.cpp.o:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/main.cpp.o
.PHONY : main.cpp.o
main.i: main.cpp.i
.PHONY : main.i
# target to preprocess a source file
main.cpp.i:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/main.cpp.i
.PHONY : main.cpp.i
main.s: main.cpp.s
.PHONY : main.s
# target to generate assembly for a file
main.cpp.s:
$(MAKE) -f CMakeFiles/a.out.dir/build.make CMakeFiles/a.out.dir/main.cpp.s
.PHONY : main.cpp.s
# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... rebuild_cache"
@echo "... a.out"
@echo "... edit_cache"
@echo "... Bdynamic_array.o"
@echo "... Bdynamic_array.i"
@echo "... Bdynamic_array.s"
@echo "... Blist.o"
@echo "... Blist.i"
@echo "... Blist.s"
@echo "... csv.o"
@echo "... csv.i"
@echo "... csv.s"
@echo "... dynamic_array.o"
@echo "... dynamic_array.i"
@echo "... dynamic_array.s"
@echo "... dynamic_array2.o"
@echo "... dynamic_array2.i"
@echo "... dynamic_array2.s"
@echo "... fs.o"
@echo "... fs.i"
@echo "... fs.s"
@echo "... list.o"
@echo "... list.i"
@echo "... list.s"
@echo "... list2.o"
@echo "... list2.i"
@echo "... list2.s"
@echo "... main.o"
@echo "... main.i"
@echo "... main.s"
.PHONY : help
#=============================================================================
# Special targets to cleanup operation of make.
# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
经过数小时无所事事,魔术已解决的问题由于某种原因,book.cpp无法编译,大约100次后,我尝试在终端中编写cmake ..它已构建并正常工作