gpt4 book ai didi

c++ - 如何解决错误 "undefined reference to pso(int, double*, double*)'收集2: ld returned 1 exit status"

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:22 24 4
gpt4 key购买 nike

我有一个名为 pso.cpp 的 C++ 程序,由一个输入和两个输出(通过指针)组成,如下所示:

void pso(int32_T 迭代,real_T *gbest,real_T *w)

我有另一个名为 main.cpp 的 C++ 程序,如下所示:

#include <math.h>
#include <stdio.h>
#include <iostream>
#include "pso.h"

using namespace std;

int main()
{
int32_T Iteration = 1000;
real_T gbest;
real_T w;
pso(Iteration, &gbest, &w);

std::cout << gbest << std::endl;
std::cout << w << std::endl;

return 0;
}

另外,pso.h如下:

#ifndef __PSO_H__

#define __PSO_H__

/* Include files */

#include <math.h>

#include <stddef.h>

#include <stdlib.h>

#include <string.h>

#include "rt_nonfinite.h"


#include "rtwtypes.h"

#include "pso_types.h"

/* Function Declarations */

extern void pso(int32_T Iteration, real_T *gbest, real_T *w);

#endif

我通过命令g++ main.cpp -o main执行main.cpp

但是我遇到了这个错误:

main.cpp:(.text+0x29): 对 pso(int, double*, double*)' 的 undefined reference
collect2:ld返回1退出状态

如何解决编程错误?

最佳答案

g++ main.cpp -o main

应该是

g++ main.cpp pso.cpp -o main

关于c++ - 如何解决错误 "undefined reference to pso(int, double*, double*)'收集2: ld returned 1 exit status",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19986326/

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