Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
When I am trying to compile the host code using make command. It gave me this error:

`/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
`


my 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;
    
 }


What I have tried:

I am not sur why the compiler does not see my main function
Posted
Updated 7-Aug-23 8:52am
v2
Comments
Richard Deeming 6-Sep-21 8:20am    
There's a secret error somewhere in your secret code. You need to fix that.
prother123 6-Sep-21 8:26am    
What do you mean please
Richard Deeming 6-Sep-21 8:26am    
An error message with no source is not enough information for anyone to help you.
prother123 6-Sep-21 8:28am    
Okay, so are you able to help me if I post my source code ?
Richard Deeming 6-Sep-21 8:37am    
Somebody might be able to. But if you don't, then nobody can.

Click the green "Improve question" link and add the relevant parts of your code to the question.

1 solution

This is a linker error, and it's telling you that the main function that it needs in order to run your code isn't there. main is the start point for any C based application, and it can't run without it.

Probably, you aren't compiling the right file, or you need to link this file output with other files - including the one containing the main function. Check for a makefile somewhere in the folder and see if that compiles everything together.

We can't help you fix this: we have no access to your system!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900