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

标签 c++ linux unix g++

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

这个项目是建立在另一个项目 Lab0 的基础上的,在 TA 的帮助下我没有遇到任何问题,但是不得不做一些奇怪的事情来构建它(#include LinkedSortedList.cpp 文件在底部链接排序列表.h)。类的每个人都用#include .cpp 文件做了这些奇怪的事情。

首先,这是文件以及它们为 Lab0 编译的#includes 的内容:

(这篇文章没有在我的 makefile 中显示我的选项卡,但它们在那里!)

生成文件:

main: *.cpp *.h

g++ -o LSL main.cpp

clean:

rm -f *.o LSL*

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

文件:

main.cpp(未模板化):

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

SortedList.h(模板化): 没有

LinkedNode.h(模板化):

#include <iostream>
using namespace std;

LinkedSortedList.h(模板化):

#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(模板化): 没有

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

这是我的问题:

下面是 lab1,我遇到了麻烦,Lab1 使用了 Lab0 的所有文件,只是添加了 Employee.h 和 Employee.cpp。

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

文件:

lab1.cpp:

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

SortedList.h(模板化): 没有

LinkedNode.h(模板化):

#include <iostream>
using namespace std;

LinkedSortedList.h(模板化):

#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(模板化): 没有

Employee.h(未模板化):

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

Employee.cpp(未模板化):

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

(这篇文章没有在我的 makefile 中显示我的选项卡,但它们在那里!)

makefile:

main: *.cpp *.h

g++ -o LSL lab1.cpp

clean:

rm -f *.o LSL*

我得到的错误:

这是我遇到的错误。似乎没有看到 Employee.cpp/Employee.h 文件。有什么想法吗??

unixserver:Lab1> make g++ -o LSL lab1.cpp /tmp/ccamnaqx.o: In function <code>createRecord()': lab1.cpp:(.text+0x3fb): undefined reference to</code>Employee::Employee()' lab1.cpp:(.text+0x477): undefined reference to <code>Employee::setLastName(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' lab1.cpp:(.text+0x4f2): undefined reference to</code>Employee::setFirstName(std::basic_string, std::allocator >)' lab1.cpp:(.text+0x589): undefined reference to <code>Employee::setId(int)' lab1.cpp:(.text+0x5ce): undefined reference to</code>Employee::setSalary(int)' lab1.cpp:(.text+0x60e): undefined reference to <code>Employee::setDepartment(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' lab1.cpp:(.text+0x67c): undefined reference to</code>Employee::setPhoneNumber(std::basic_string, std::allocator >)' lab1.cpp:(.text+0x6ea): undefined reference to <code>Employee::setAddress(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' lab1.cpp:(.text+0x758): undefined reference to</code>Employee::setHireDate(std::basic_string, std::allocator >)' lab1.cpp:(.text+0x7c6): undefined reference to `Employee::setEmailAddress(std::basic_string, std::allocator >)' collect2: ld returned 1 exit status make: <em>*</em> [main] Error 1

如有任何帮助,我们将不胜感激!

最佳答案

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

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

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

关于c++ - 如何让我的 C++ 程序在 Unix 上编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10367367/

相关文章:

linux - 在 Linux 中遍历进程的页表

json - 使用 bash 找到匹配的字符串键并在 json 文件中增加其值

json - Unix 或 Linux 上的 CURL 报告为二进制文件并显示控制字符,而不是响应 header 所说的内容,并且在 Windows 上没有问题

c++ - 从 vector<string> 转换为 wchar_t**

c++ - 如何让我的程序循环发出蜂鸣声?

c++ - winapi:从 HDC 到 HBITMAP

c - 用于嵌入式系统中 RS232 设备的 Linux 设备驱动程序

c++ - 将 condition_variable 与 unique_lock 一起使用导致周期性崩溃(GCC 4.7,OSX)

linux - 将非连续文件移动到新目录

debugging - 彩色 grep 的等价物是什么?