[英]Issues with compiling multiple files
因此,我尝试编写一些代码来使用数独难题,并且在尝试编译时不断出错,并且每次编译时都会删除驱动程序。
这是驱动程序的代码:
/*
* Jared C
* C++ Project
*/
#include <iostream>
#include "SudokuBoard.h"
using namespace std;
int main()
{
SudokuBoard board("sudoku.txt");
board.printBoard();
}
这是头文件
/*
* Jared C
* SudokuBoard.h
*/
#ifndef __SUDOKUBOARD_H_INCLUDED__
#define __SUDOKUBOARD_H_INCLUDED__
#include <iostream>
#include <string>
class SudokuBoard
{
private:
int content[9][9];
public:
SudokuBoard(std::string filename);
void printBoard();
int getNumAt(int, int);
void setNumAt(int, int, int);
};
#endif
最后是Sudoku Board.cpp
/*
* Jared C
* SudokuBoard.cpp
*/
#include <iostream>
#include <fstream>
#include "stdlib.h"
#include "SudokuBoard.h"
using namespace std;
SudokuBoard::SudokuBoard(string filename)
{
string output;
string line;
int count = 0;
ifstream file;
file.open(filename.c_str());
if (file.is_open())
{
while (getline (file, line))
{
output += line;
}
file.close();
}
else cout<< "unable to open file" << endl;
for(int y = 0; y < 9; y++)
{
for(int x = 0; x < 9; x++)
{
content[x][y] = atoi(output.substr(count,1).c_str());
count ++;
}
}
}
void SudokuBoard::printBoard()
{
string output = "\n";
for(int y = 0; y < 9; y++)
{
if(y%3==0)
{
output += '\n';
}
for(int x = 0; x < 9; x++)
{
if(x%3==0)
{
output += " ";
}
output += content[x][y];
output += "\n";
}
}
cout<<output<<endl;
}
int SudokuBoard::getNumAt(int x, int y)
{
return content[x][y];
}
void SudokuBoard::setNumAt(int x, int y, int value)
{
content[x][y] = value;
}
当我调用gcc -c SudokuBoard.cpp
我会得到SudokuBoard.o文件,但是当我再调用'gcc -o Driver.cpp SudokuBoard.o'时,我会收到大量错误消息,以下是其中的一个示例:
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
SudokuBoard.cpp:(.text+0x34): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
SudokuBoard.cpp:(.text+0x43): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
并删除Driver.cpp知道我在做什么错吗?
您的命令行不正确:
gcc -o Driver.cpp SudokuBoard.o
指示gcc
将目标文件SudokuBoard.o链接为名为Driver.cpp
的可执行文件。 毫不奇怪,它首先会擦除目标文件。
此外,您未指定要与之链接的运行时库,并且gcc
并非默认为C ++:这说明了错误消息。
您应该改写:
g++ -o Sudoku Driver.o SudokuBoard.o
使用g++
编译C ++代码,而不是通用的面向C的gcc
。
g++
调用gcc
进行编译,并使用C ++的正确选项进行链接。
简而言之,最好将C ++编译器用于C ++源代码,将Fortran编译器用于Fortran源代码,等等。
另外,对于“并且它会删除Driver.cpp”的问题,只需不要将源文件指定为输出文件。
编译器告诉您找不到std::basic_string<...>
。
您需要告诉编译器如何找到std::basic_string<...>
的已编译代码。 对于您自己的代码,如果尝试仅从Driver.cpp
构建可执行文件,编译器将抱怨缺少SudokuBoard
引用,直到您将其告知SudokuBoard.o
。
在这种情况下,您需要告诉编译器有关C ++标准库的信息。 该库通常随您的发行版一起提供。 在Windows上以dll
文件的形式出现,在OS X dylib
文件和Linux上是so
文件。 从概念上讲,他们是相似的,很多袋.o
文件都在一起。 无论哪种方式,您都告诉链接器链接到C ++标准库。
gcc
调用C ++标准库libstdc++
,因此您的命令将类似于: gcc -o Driver Driver.cpp SudokuBoard.o -lstdc++
。
问题是gcc的-o选项用于指定输出文件。 如果你写
gcc -o Driver.cpp SudokuBoard.o
您告诉编译器从目标文件SudokuBoard.o中创建一个名为Driver.cpp的可执行文件。 但是由于SudokuBoard.o没有主要方法,因此将失败,因此会出现错误消息。 尝试
gcc -c SudokuBoard.cpp
gcc -c Driver.cpp
gcc -o Sudoku SudokuBoard.o Driver.o
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.