如何让我的C++程序在 Unix 上编译

How can I get my C++ program to compile on Unix?

本文关键字:Unix 编译 程序 C++ 我的      更新时间:2023-10-16

我正在C++从事一个学校项目,并按照教授的指示使用我们学校的Unix服务器编写它。我正在使用 vim 和 g++ 进行编译。

这个项目是在另一个项目Lab0的基础上构建的,在TA的帮助下,我毫不费力地构建了它,但不得不做一些奇怪的事情来构建它(#include LinkedSortedList.cpp文件在LinkedSortedList.h的底部(。班上的每个人都用 #include .cpp 文件做了这种奇怪的事情。

首先,以下是文件以及它们对编译良好的 Lab0 #includes:

(该帖子没有在我的制作文件中显示我的选项卡,但它们就在那里!

制作文件:

main: *.cpp *.h
g++ -o LSL main.cpp
clean:
rm -f *.o LSL*

Lab0(构建的那个(是这样的:

文件:

主.cpp(非模板化(:

#include "LinkedSortedList.h"
#include <iostream>
using namespace std;

SortedList.h (Templated(: 无

LinkedNode.h (Templated(:

#include <iostream>
using namespace std;

LinkedSortedList.h (Templated(:

#include "SortedList.h"
#include "LinkedNode.h"
#include <iostream>
using namespace std;
#include "LinkedSortedList.cpp" // At the bottom fo this file above the #endif to get the program to compile from what the TA told me to do for lab0 due to the templated class.

LinkedSortedList.cpp (Templated(: 无

构建和运行此项目没有问题。

这是我的问题:

下面是 lab1,我遇到问题的那个,Lab1 使用 Lab0 中的所有文件,只是添加了 Employee.h 和 Employee.cpp。

Lab1(不会构建的那个(是这样的:

文件:

实验1.cpp:

#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "Employee.h"
#include "LinkedSortedList.h"

SortedList.h (Templated(: 无

LinkedNode.h (Templated(:

#include <iostream>
using namespace std;

LinkedSortedList.h (Templated(:

#include "SortedList.h"
#include "LinkedNode.h"
#include "Employee.h"
#include <iostream>
using namespace std;
#include "LinkedSortedList.cpp" // At the bottom fo this file above the #endif to get the program to compile from what the TA told me to do for lab0 due to the templated class.

LinkedSortedList.cpp (Templated(: 无

员工.h (非模板化(:

#include <iostream>
#include <sstream>
using namespace std;

员工.cpp(非模板(:

#include <stdio.h>
#include <string.h>

(该帖子没有在我的制作文件中显示我的选项卡,但它们就在那里!

makefile:
main: *.cpp *.h
g++ -o LSL lab1.cpp
clean:
rm -f *.o LSL*

我得到的错误:

这是我遇到的错误。似乎看不到 Employee.cpp/Employee.h 文件。任何想法??

Unixserver:Lab1> makeg++ -o LSL lab1.cpp/tmp/ccamnaqx.o: 在函数中 createRecord()': lab1.cpp:(.text+0x3fb): undefined reference to Employee::Employee(('lab1.cpp:(.text+0x477(: 未定义对Employee::setLastName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' lab1.cpp:(.text+0x4f2): undefined reference to Employee::setFirstName(std::basic_string, std::分配器>('lab1.cpp:(.text+0x589(:未定义对Employee::setId(int)' lab1.cpp:(.text+0x5ce): undefined reference to Employee::setSalary(int('lab1.cpp:(.text+0x60e(:未定义对Employee::setDepartment(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' lab1.cpp:(.text+0x67c): undefined reference to Employee::setPhoneNumber(std::basic_string, std::分配器>('lab1.cpp:(.text+0x6ea(: 未定义对Employee::setAddress(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' lab1.cpp:(.text+0x758): undefined reference to Employee::setHireDate(std::basic_string, std::分配器>('lab1.cpp:(.text+0x7c6(:未定义对"Employee::setEmailAddress(std::basic_string, std:::allocator>("的引用收集2:LD 返回 1 个退出状态制造: * [主要] 错误 1

任何帮助将不胜感激!

您必须编译Employee.cpp并将其链接到可执行文件中:

g++ -o LSL lab1.cpp Employee.cpp

LinkedSortedList.cpp可能也是如此(我并不完全清楚它的目的(。

第二个项目的生成文件仅编译 lab1.cpp而不编译其他 cpp 文件。 您还有另外两个(Employee.cpp和LinkSortedList.cpp(需要考虑。

另外,请在此站点上了解如何正确格式化代码。 https://stackoverflow.com/editing-help