gpt4 book ai didi

c - 编译器 gcc 错误 : undefined reference to `main'

转载 作者:行者123 更新时间:2023-11-30 21:41:52 25 4
gpt4 key购买 nike

我正在用 C 语言编写 K_ 均值聚类程序,并在 Linux 中的 GCC 编译器上编译该程序编译器错误信息如下

***/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status***

我的完整程序如下

#include <stdlib.h>
#include <assert.h>
#include <float.h>
#include <math.h>

int *k_means(double **data, int n, int m, int k, double t, double
**centroids) { /* output cluster label for each data point */ int *labels = (int*)calloc(n, sizeof(int));

int h, i, j; /* loop counters, of course :) */ int *counts =
(int*)calloc(k, sizeof(int)); /* size of each cluster */ double
old_error, error = DBL_MAX; /* sum of squared euclidean distance */
double **c = centroids ? centroids : (double**)calloc(k,
sizeof(double*)); double **c1 = (double**)calloc(k,
sizeof(double*)); /* temp centroids */

assert(data && k > 0 && k <= n && m > 0 && t >= 0); /* for
debugging */

/**** ** initialization */

for (h = i = 0; i < k; h += n / k, i++) {
c1[i] = (double*)calloc(m, sizeof(double));
if (!centroids) {
c[i] = (double*)calloc(m, sizeof(double));
}
/* pick k points as initial centroids */
for (j = m; j-- > 0; c[i][j] = data[h][j]); }

/**** ** main loop */

do {
/* save error from last step */
old_error = error, error = 0;

/* clear old counts and temp centroids */
for (i = 0; i < k; counts[i++] = 0) {
for (j = 0; j < m; c1[i][j++] = 0);
}

for (h = 0; h < n; h++) {
/* identify the closest cluster */
double min_distance = DBL_MAX;
for (i = 0; i < k; i++) {
double distance = 0;
for (j = m; j-- > 0; distance += pow(data[h][j] - c[i][j], 2));
if (distance < min_distance) {
labels[h] = i;
min_distance = distance;
}
}
/* update size and temp centroid of the destination cluster */
for (j = m; j-- > 0; c1[labels[h]][j] += data[h][j]);
counts[labels[h]]++;
/* update standard error */
error += min_distance;
}

for (i = 0; i < k; i++) { /* update all centroids */
for (j = 0; j < m; j++) {
c[i][j] = counts[i] ? c1[i][j] / counts[i] : c1[i][j];
}
}

} while (fabs(error - old_error) > t);

/**** ** housekeeping */

for (i = 0; i < k; i++) {
if (!centroids) {
free(c[i]);
}
free(c1[i]); }

if (!centroids) {
free(c); } free(c1);

free(counts);

return labels; }

请帮助我们。

#include <stdlib.h>
#include <assert.h>
#include <float.h>
#include <math.h>

int *k_means(double **data, int n, int m, int k, double t, double
**centroids) { /* output cluster label for each data point */ int *labels = (int*)calloc(n, sizeof(int));

int h, i, j; /* loop counters, of course :) */ int *counts =
(int*)calloc(k, sizeof(int)); /* size of each cluster */ double
old_error, error = DBL_MAX; /* sum of squared euclidean distance */
double **c = centroids ? centroids : (double**)calloc(k,
sizeof(double*)); double **c1 = (double**)calloc(k,
sizeof(double*)); /* temp centroids */

assert(data && k > 0 && k <= n && m > 0 && t >= 0); /* for
debugging */

/**** ** initialization */

for (h = i = 0; i < k; h += n / k, i++) {
c1[i] = (double*)calloc(m, sizeof(double));
if (!centroids) {
c[i] = (double*)calloc(m, sizeof(double));
}
/* pick k points as initial centroids */
for (j = m; j-- > 0; c[i][j] = data[h][j]); }

/**** ** main loop */

do {
/* save error from last step */
old_error = error, error = 0;

/* clear old counts and temp centroids */
for (i = 0; i < k; counts[i++] = 0) {
for (j = 0; j < m; c1[i][j++] = 0);
}

for (h = 0; h < n; h++) {
/* identify the closest cluster */
double min_distance = DBL_MAX;
for (i = 0; i < k; i++) {
double distance = 0;
for (j = m; j-- > 0; distance += pow(data[h][j] - c[i][j], 2));
if (distance < min_distance) {
labels[h] = i;
min_distance = distance;
}
}
/* update size and temp centroid of the destination cluster */
for (j = m; j-- > 0; c1[labels[h]][j] += data[h][j]);
counts[labels[h]]++;
/* update standard error */
error += min_distance;
}

for (i = 0; i < k; i++) { /* update all centroids */
for (j = 0; j < m; j++) {
c[i][j] = counts[i] ? c1[i][j] / counts[i] : c1[i][j];
}
}

} while (fabs(error - old_error) > t);

/**** ** housekeeping */

for (i = 0; i < k; i++) {
if (!centroids) {
free(c[i]);
}
free(c1[i]); }

if (!centroids) {
free(c); } free(c1);

free(counts);

return labels; }

最佳答案

如果保留整个代码的代码格式,而不仅仅是其一部分,那么可读性会更高。

但是当我尝试寻找 main 函数时,却没有找到。因此,没有对 main 的引用,因为您还没有实现它,至少在您在此发布的代码中没有实现。

关于c - 编译器 gcc 错误 : undefined reference to `main' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30752494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com