-1

I have a c++ program with the name pso.cpp by the one input and two outputs (by pointer) as follows:

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

I have another c++ program with the name main.cpp as follows:

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

Also, the pso.h is as follows:

#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

I execute the main.cpp by command g++ main.cpp -o main.

But i faced with this error:

main.cpp:(.text+0x29): undefined reference to pso(int, double*, double*)' collect2: ld returned 1 exit status

How can i solve the programming error?

BlueBit
  • 397
  • 6
  • 22
  • This isn't the problem, but names that contain two consecutive underscores (`__PSO_H__`) and names that begin with an underscore followed by a capital letter are reserved to the implementation. Don't use them. – Pete Becker Nov 14 '13 at 19:19
  • I removed two consecutive underscores in all .h files. But you said about names with beginning of underscore. Is there any variable by this structure in my coding? – BlueBit Nov 14 '13 at 19:29
  • There's nothing like that in the code you posted. – Pete Becker Nov 14 '13 at 19:30
  • Unfortunately, the same error is occurred. The problem is not two consecutive underscores. I am with this error at least 12h :-( – BlueBit Nov 14 '13 at 19:41
  • 1
    As I said originally: "This **isn't** the problem, but ..." – Pete Becker Nov 14 '13 at 19:43
  • Please read much more about how to use `g++` (i.e. how to [invoke GCC](http://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html) ...) and how to use [GNU make](http://www.gnu.org/software/make/) and how to compile and link a program. – Basile Starynkevitch Nov 14 '13 at 20:18
  • @BasileStarynkevitch , i read previously the documentation, but i could not solve the problem. It is a why that i ask this question. If your read the documentation and know better than us, please propose a solution. – BlueBit Nov 14 '13 at 20:34
  • I have no idea about your entire code. What libraries are you using? What are the headers like `"rtwtypes.h"`.... You are lacking some basic skills... See e.g. [this](http://stackoverflow.com/a/18877584/841108) – Basile Starynkevitch Nov 14 '13 at 20:36
  • BTW, a C++ source file (or more pedantically [translation unit](http://en.wikipedia.org/wiki/Translation_unit_%28programming%29) ...) is not a C++ program (because most software written in C++ have many source files, compiled then linked into one single binary program executable). – Basile Starynkevitch Nov 14 '13 at 20:46

1 Answers1

2
g++ main.cpp -o main

should be

g++ main.cpp pso.cpp -o main
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • I ran your suggested command that i face with new error: /tmp/ccSq08Uh.o: In function `pso(int, double*, double*)': pso.cpp:(.text+0xeb): undefined reference to `rtInf' pso.cpp:(.text+0x12b): undefined reference to `b_rand()' pso.cpp:(.text+0x1c8): undefined reference to `Sphere(double const*)' pso.cpp:(.text+0x32e): undefined reference to `c_rand(double*)' pso.cpp:(.text+0x339): undefined reference to `c_rand(double*)' pso.cpp:(.text+0x864): undefined reference to `Sphere(double const*)' collect2: ld returned 1 exit status – BlueBit Nov 14 '13 at 19:49
  • You probably need to add libraries, e.g. some `-lpso` after the last `*.cpp` file in your `g++` command. You should learn much more about how to use `g++`. Order of arguments to `g++` matters a lot. And using `-Wall -g` should help. At last, learn how to use [GNU make](http://www.gnu.org/software/make/) - since you have several source files... – Basile Starynkevitch Nov 14 '13 at 20:13
  • @BlueBit: notice that `b_rand` (or `c_rand`) is not a [standard C++](http://en.cppreference.com/w/cpp) function. Probably some header declares it and some library provides it. You only know which, we can't guess it (some Matlab thing perhaps????) – Basile Starynkevitch Nov 14 '13 at 20:51
  • Dear friends, my problem was solved! I just call all the `*.cpp` files (functions) into main program. Do you think about this kind of calling? Is it common? or just `*.h` should be called? But, for my program calling `*.cpp` programs were enough. :-) – BlueBit Nov 16 '13 at 12:00
  • @BlueBit - it's sort-of common, but it's wrong. C++ supports **separate compilation**: each source file stands on its own, so you only have to recompile files that changed. When you put everything into a single file (which is the effect of `#include`ing all your source files) you recompile everything every time. While that's not a big deal on small projects, on larger ones it makes everything much slower and harder to understand. – Pete Becker Nov 16 '13 at 14:29