【解決方法】「「メイン」collect2 への未定義参照: エラー: ld が 1 終了ステータスを返しました」を修正する方法

プログラミングQA


make コマンドを使用してホスト コードをコンパイルしようとしているとき。 それは私にこのエラーを与えました:

`/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [bin/host] Error 1
`

私のmain.cpp

#include<assert.h>
#include<math.h>
#include<cstring>
//#include "AOCLUtils/aocl_utils.h"
//#include "stdafx.h"
#include<CL/cl.h>
#include<time.h>
//#include<sys/time.h>
#include<CL/cl_ext.h>
#include<algorithm>
#include<iomanip>
#include<iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <array> 
#include <string.h>
#include <CL/opencl.h>

using namespace std;
using namespace aocl_utils;
void cleanup() {}

bool read_data_set(string filename, array<array<int, 20>, 5430>& array_X_dataset, array<int, 5430>& array_Y_dataset) {
    int field0, field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11,
        field12, field13, field14, field15, field16, field17, field18, field19, field20, field21;
    char comma;
    int line = 0;

    ifstream myfile(filename);

    if (myfile.is_open())
    {
        while (myfile
            >> field0 >> comma
            >> field1 >> comma
            >> field2 >> comma
            >> field3 >> comma
            >> field4 >> comma
            >> field5 >> comma
            >> field6 >> comma
            >> field7 >> comma
            >> field8 >> comma
            >> field9 >> comma
            >> field10 >> comma
            >> field11 >> comma
            >> field12 >> comma
            >> field13 >> comma
            >> field14 >> comma
            >> field15 >> comma
            >> field16 >> comma
            >> field17 >> comma
            >> field18 >> comma
            >> field19 >> comma
            >> field20 >> comma
            >> field21)
        {


            array<int, 20> inner_array{ field1, field2, field3, field4, field5, field6, field7, field8, field9, field10, field11,
            field12, field13, field14, field15, field16, field17, field18, field19, field20 };
            array_X_dataset[line] = inner_array;
            array_Y_dataset[line] = field21;
            line++;

        }

        myfile.close();

    }
    else {
        cout << "Unable to open file";
        return true;
    }
    return false;
}

//functoin to randomly mix the dataset.
void mix_dataset(array<array<int, 20>, 5430>& array_X_dataset, array<int, 5430>& array_Y_dataset) {
    size_t len = array_X_dataset.size();
    for (size_t i = 0; i < len; ++i) {
        size_t swap_index = rand() % len;  // Random number between 0 and len-1.
        if (i == swap_index)
            continue;

        array<int, 20> data_point{  };
        data_point = array_X_dataset[i];
        array_X_dataset[i] = array_X_dataset[swap_index];
        array_X_dataset[swap_index] = data_point;
        int Y = array_Y_dataset[i];
        array_Y_dataset[i] = array_Y_dataset[swap_index];
        array_Y_dataset[swap_index] = Y;
    }
}



//functoin to divide the dataset using 5-fold cross validatoin. 
void split_dataset(int fold, int **array_X_set, int *array_Y_set,int **X_train, int *Y_train,
    int **X_test, int *Y_test) {
   int rows = 5430;
    int cols = 20;
    int division = 1086;
    switch (fold) {
    case 1:
              
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (i < division) {
                    X_test[i][j] = array_X_set[i][j];
                    Y_test[i] = array_Y_set[i];
                }

                else {
                    X_train[i - division][j] = array_X_set[i][j];
                    Y_train[i - division] = array_Y_set[i];
                }
            }
        }
        break;

    case 2:
        // apply loop unrolling (#pragma unroll <N>)
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (i >= 1086 && i <= 2171) {
                    X_test[i - 1086][j] = array_X_set[i][j];
                    Y_test[i - 1086] = array_Y_set[i];
                }
                else {
                    if (i < 1086) {
                        X_train[i][j] = array_X_set[i][j];
                        Y_train[i] = array_Y_set[i];
                    }
                    else {
                        X_train[i - (2171 - 1086 + 1)][j] = array_X_set[i][j];
                        Y_train[i - (2171 - 1086 + 1)] = array_Y_set[i];
                    }
                }
            }
        }
        break;

    case 3:
        // apply loop unrolling (#pragma unroll <N>)
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (i >= 2172 && i <= 3257) {
                    X_test[i - 2172][j] = array_X_set[i][j];
                    Y_test[i - 2172] = array_Y_set[i];
                }

                else {
                    if (i < 2172) {
                        X_train[i][j] = array_X_set[i][j];
                        Y_train[i] = array_Y_set[i];
                    }
                    else
                    {
                        X_train[i - (3257 - 2172 + 1)][j] = array_X_set[i][j];
                        Y_train[i - (3257 - 2172 + 1)] = array_Y_set[i];

                    }
                }
            }
        }
        break;

    case 4:
        // apply loop unrolling (#pragma unroll <N>)
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (i >= 3258 && i <= 4343) {
                    X_test[i - 3258][j] = array_X_set[i][j];
                    Y_test[i - 3258] = array_Y_set[i];
                }

                else {
                    if (i < 3258) {
                        X_train[i][j] = array_X_set[i][j];
                        Y_train[i] = array_Y_set[i];
                    }
                    else
                    {
                        X_train[i - (4343 - 3258 + 1)][j] = array_X_set[i][j];
                        Y_train[i - (4343 - 3258 + 1)] = array_Y_set[i];

                    }
                }
            }
        }
        break;
    case 5:
        // apply loop unrolling (#pragma unroll <N>)
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                if (i >= 4344 && i <= 5429) {
                    X_test[i - 4344][j] = array_X_set[i][j];
                    Y_test[i - 4344] = array_Y_set[i];
                }

                else {
                    if (i < 4344) {
                        X_train[i][j] = array_X_set[i][j];
                        Y_train[i] = array_Y_set[i];
                    }
                    else
                    {
                        X_train[i - (5429 - 4344 + 1)][j] = array_X_set[i][j];
                        Y_train[i - (5429 - 4344 + 1)] = array_Y_set[i];

                    }
                }
            }
        }
        break;
    }

}



int main()
{
    // Read dataset from file.
    string filename = ".//dataset.csv";
    static array<array<int, 20>, 5430> array_X_dataset{};
    static array<int, 5430> array_Y_dataset{};
   
    bool error = read_data_set(filename, array_X_dataset, array_Y_dataset);
    if (error) {
        cout << "Exiting with error while reading dataset file " << filename << endl;
        exit(-1);
    }
    //printout whole dataset & size of feature dataset:  
    /*  for (int i = 0; i < 5430; i++) {
       printf ( " %d ", i);
       for (int j = 0; j < 20 ;j++)          
           printf(" % d ", array_X_dataset[i][j]);
             printf(" %d ", array_Y_dataset[i]);
       printf("\n");
            
   }*/


    // Randomly mix the dataset and printout.
    // Initialize the seed.
    srand(3);
    mix_dataset(array_X_dataset, array_Y_dataset);

    //int array_X_set[5430][20];
   // int array_Y_set[5430];
     
    int* array_Y_set = new int[5430];
    int** array_X_set = new int* [5430];
    for (int i = 0; i < 5430; i++) {
        array_X_set[i] = new int[20];
    }
    // copy contents of the mixed std::arrays into plain arrays  
    for (int i = 0; i < 5430; i++) {
        for (int j = 0; j < 20; j++)
            array_X_set[i][j] = array_X_dataset[i][j];
        array_Y_set[i] = array_Y_dataset[i];
    }
  /*  printf("printout the whole dataset after random mixing:\n");
     for (int i = 0; i < 5430; i++) {
        printf ( " %d ", i);
        for (int j = 0; j < 20 ;j++)
            printf(" %d ", array_X_set[i + j * 5430]);
              printf(" %d ", array_Y_set[i]);
        printf("\n");

    }*/

    
    int* Y_train = new int[4344];
    int* Y_test = new int[1086];

    int** X_train = new int* [4344];
    for (int i = 0; i < 4344; i++) {
        X_train[i] = new int[20];
    }
    int** X_test = new int* [1086];
    for (int i = 0; i < 1086; i++) {
        X_test[i] = new int[20];
    }
    //split the dataset using 5 - fold cross validation
    int fold = 1;    
   // cout << "inseret fold num " << endl;
     //cin >> fold;
    split_dataset(fold, array_X_set, array_Y_set, X_train, Y_train, X_test, Y_test);
   // int x;
   // cin >> x;
    // printout the train  dataset
   /* for (int i = 0; i < 4344; i++) {
        printf(" %d ", i);
        for (int j = 0; j < 20; j++)
            printf(" %d ", X_train[i][j]);
        printf(" %d ", Y_train[i]);
        printf("\n");
      
    }*/
    // printout the test dataset
   /* for (int i = 0; i < 1086; i++) {
        printf(" %d ", i);
        for (int j = 0; j < 20; j++)
            printf(" %d ", X_test[i][j]);
        printf(" %d ", Y_test[i]);
        printf("\n");
    }*/

    // deallocate memory using the delete operator
    /*for (int i = 0; i < 5430; i++) {
        delete[] array_X_set[i];
    }
    delete[] array_X_set;
    for (int i = 0; i < 4344; i++) {
        delete[] X_train[i];
    }
    delete[] X_test;
    for (int i = 0; i < 1086; i++) {
        delete[] X_test[i];
    }
    delete[] X_train;
  
    delete[] array_Y_set;
    delete[] Y_train;
    delete[] Y_test;*/
    //--------------------------host code--------------------------------------------------------------//

    // Search for an openCL platform
    cl_platform_id fpga_paltform = NULL;
    if (clGetPlatformIDs(1, &fpga_paltform, NULL) != CL_SUCCESS) {
        printf("Unable to get platform_id\n");
        return 1;
    }

    // Search for an openCL device
    cl_device_id fpga_device = NULL;
    if (clGetDeviceIDs(fpga_paltform, CL_DEVICE_TYPE_ALL, 1, &fpga_device, NULL) != CL_SUCCESS) {
        printf("Unable to get device_id\n");
        return 1;
    }

    // Create a context.
    cl_context context = clCreateContext(NULL, 1, &fpga_device, NULL, NULL, NULL);

    // Create a command queue using the context and device.
    cl_command_queue queue = clCreateCommandQueue(context, fpga_device, 0, NULL);

    // Read FPGA binary
    size_t length = 0x10000000;
    unsigned char* binary = (unsigned char*)malloc(length);
    FILE* fp = fopen("kernel.aocx", "rb");
    fread(binary, length, 1, fp);
    fclose(fp);

    // Create program
    cl_program program = clCreateProgramWithBinary(context, 1, &fpga_device, &length, (const unsigned char**)&binary, NULL, NULL);

    // Create kernel.
    cl_kernel kernel = clCreateKernel(program, "KNN_classifier", NULL); 


    // Create memory Object :: create buffers for the input and ouput
    // inputs: X_train, Y_train, data_point, k
    // outputs: host_output

    int host_output[4344];// back
    int k = 3;  
    int data_point[20] = {};
    for (int i = 0; i < 4344; ++i) {
        for (int j = 0; j < 20; ++j) {
            data_point[j] = array_X_set[i][j];
        }
    }

    cl_mem dev_X_train = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * 4344 * 20, X_train, NULL);
    cl_mem dev_Y_train = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * 4344, Y_train, NULL);
    cl_mem dev_data_point = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int) * 20, data_point, NULL);
    cl_mem dev_output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int) * 4344, host_output, NULL);

    // Trnsfer input data from host to device
    clEnqueueWriteBuffer(queue, dev_X_train, CL_TRUE, 0, sizeof(int) * 4344 * 20, X_train, 0, NULL, NULL);
    clEnqueueWriteBuffer(queue, dev_Y_train, CL_TRUE, 0, sizeof(int) * 4344, Y_train, 0, NULL, NULL);
    clEnqueueWriteBuffer(queue, dev_data_point, CL_TRUE, 0, sizeof(int) * 20, data_point, 0, NULL, NULL);

    // Set kernel arguments
    clSetKernelArg(kernel, 0, sizeof(cl_mem), &dev_X_train);
    clSetKernelArg(kernel, 1, sizeof(cl_mem), &dev_Y_train);
    clSetKernelArg(kernel, 2, sizeof(cl_mem), &dev_data_point);
    clSetKernelArg(kernel, 3, sizeof(int), &k);
    clSetKernelArg(kernel, 4, sizeof(cl_mem), &dev_output);


    // Execute kernel
    cl_event kernel_event;
    clEnqueueTask(queue, kernel, 0, NULL, &kernel_event);
    clWaitForEvents(1, &kernel_event);
    clReleaseEvent(kernel_event);

    // Read data from FPGA
    clEnqueueReadBuffer(queue, dev_output, CL_TRUE, 0, sizeof(int) * 4344, host_output, 0, NULL, NULL);

    // Print output
    for (int i = 0; i < 4344; i++)
        printf("class_label[%d] = %d\n", i, host_output[i]);


    clFlush(queue);
    clFinish(queue);
    // Free memory
    clReleaseMemObject(dev_X_train);
    clReleaseMemObject(dev_Y_train);
    clReleaseMemObject(dev_data_point);
    clReleaseMemObject(dev_output);
    clReleaseKernel(kernel);
    clReleaseProgram(program);
    clReleaseCommandQueue(queue);
    clReleaseContext(context);

   // free(X_train);
    //free(Y_train);
    
    // deallocate memory using the delete operator
    for (int i = 0; i < 5430; i++) {
        delete[] array_X_set[i];
    }
    delete[] array_X_set;
    for (int i = 0; i < 4344; i++) {
        delete[] X_train[i];
    }
    delete[] X_test;
    for (int i = 0; i < 1086; i++) {
        delete[] X_test[i];
    }
    delete[] X_train;

    delete[] array_Y_set;
    delete[] Y_train;
    delete[] Y_test;
    free(data_point);
    free(host_output);

    return 0;
    
 }

私が試したこと:

コンパイラがメイン関数を認識しない理由がわかりません

解決策 1

これはリンカ エラーであり、 main コードを実行するために必要な機能がありません。 main は C ベースのアプリケーションの出発点であり、それなしでは実行できません。

おそらく、正しいファイルをコンパイルしていないか、このファイル出力を他のファイルとリンクする必要があります。 main 関数。 を確認してください makefile フォルダーのどこかで、すべてが一緒にコンパイルされるかどうかを確認します。

これを修正するお手伝いはできません。システムにアクセスできません!

解決策 3

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
void linklisttraversal(struct node*head)
{
struct node*temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
void main()
{
struct node*n1;
struct node*n2;
struct node*n3;
n1=(struct node*)malloc(sizeof(struct node));
n2=(struct node*)malloc(sizeof(struct node));
n3=(struct node*)malloc(sizeof(struct node));
n1->data=20;
n1->next=n2;
n2->data=30;
n2->next=n3;
n3->data=40;
n3->next=NULL;
linklisttraversal(head);
}
}

コメント

タイトルとURLをコピーしました