为什么源文件的名称会影响编译

Why does the name of a source file affect compilation?

本文关键字:影响 编译 源文件 为什么      更新时间:2023-10-16

当我用C++编写一个名为"lotto.cpp"的小lotto程序时,我遇到了一些非常奇怪的事情。在我为程序编写写入文件之前,一切都很好。当我编译时,它显示了以下错误:

ld: can't open output file for writing: lotto, errno=21 for architecture x86_64
collect2: ld returned 1 exit status

巧合的是,我把程序的名称改为"1.cpp",突然间,它编译得毫无问题。当我将名称更改为"test.cpp"时,它也起了作用

我真的很好奇为什么会发生这种事。有什么想法吗?

这发生在MacBook Pro上。

如果你也想要代码,请告诉我!


我知道有些人要密码。这是:

#include <iostream>
#include <fstream>
using namespace std;
const int NED  = 10;            
const int VIKING =  6;
const int NORMAL =  7;
const int MAX  = 10;
void quickSort(int arr[], int left, int right);
int checkDuplicates(int arr[], int length);
int main (int argc, const char *argv[])
{
    int i, j, k, ans;
    char ans2;
    int lottoNumbers[MAX];
    ofstream out("Lotto.txt", ios::out | ios::app);
    srand((unsigned)time(NULL));    
    do
    {
        do
        {
            cout << "nnDo you want to play Viking Lotto (press 6), or normal Lotto (press 7): ";
            cin >> ans;
        }while(ans != VIKING && ans != normal);
        (ans == VIKING) ? cout << "nViking Lotto:n" : cout << "nnnormal Lotto:n";
        (ans == VIKING) ? out << "nViking Lotto:n" : out << "nnnormal Lotto:n";

        for (i = 0; i < NED; i++)       //10 rows
        {       
            for (j = 0; j < ans; j++)  //6 or 7 columns
            {
                (ans == VIKING) ? lottoNumbers[j] = (rand() % 48) + 1 : lottoNumbers[j] = (rand() % 34) + 1;
            }
            if(checkDuplicates(lottoNumbers, ans) != -1)        
            {
                for(k = 0; k < ans; k++)
                {
                    while(checkDuplicates(lottoNumbers, ans) == lottoNumbers[k])    
                    {
                        (ans == VIKING) ? lottoNumbers[k] = (rand() % 48) + 1 : lottoNumbers[k] = (rand() % 34) + 1;
                    }
                }
            }
            quickSort(lottoNumbers, 0, ans - 1);  
            cout << 'n';
            for(j = 0; j < ans; j++)
            {
                cout << lottoNumbers[j] << 't'; 
                out << lottoNumbers[j] << 't';
            }
            out << 'n';
        }   
        cout << "nn";

        cout <<"Another lottery ticket (Y/N) ";
        cin >> ans2;
    }while(ans2 == 'j' || ans2 == 'J');
    cout << "nnLOTTO NUMBERS WAS WRITTEN TO FILE...nn";
    return 0;
}
void quickSort(int arr[], int left, int right) 
{
    int i = left, j = right;
    int tmp;
    int mid = arr[(left + right) / 2];
    while (i <= j) 
    {
        while (arr[i] < mid)    i++;
        while (arr[j] > mid)    j--;
        if (i <= j) 
        {
            tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    };
    if (left < j)   quickSort(arr, left, j);
    if (i < right)  quickSort(arr, i, right);
}
int checkDuplicates(int arr[], int length)  
{                                       
    for(int i = 0; i < length; i++)
    {
        for(int j = i + 1; j < length; j++)
        {   
            if(arr[i] == arr[j])    return arr[j];
        }
    }
    return -1;  
}

错误编号21(在MacOS X 10.7.2上)为EISDIR: Is a directory

名称lotto似乎是一个目录,而不是一个文件。

这是一个链接器错误,说明我们在编译时无法写入您计算机上的"lotto"文件。我的猜测是,要么你的程序仍在运行,要么你意外地创建了一个名为"lotto"的目录。可能是您的文件写入函数使应用程序保持运行,或者它本身试图创建一个lotto目录。

是的,我在将visual studio的一些代码复制到mac时遇到了这个问题。Visual Studio似乎喜欢在你的项目中使用你的可执行文件名创建文件夹,这就是为什么!

FWIW当我试图将输出文件写入尚未创建的目录(即bin/myprogram)时,出现了此错误。

一旦我创建了bin目录,一切都很好;我不需要重新命名任何东西。GCC似乎会在目录不存在的情况下创建该目录,而clang则不会(至少我能说的很接近)。