Hello, everyone! It's been a long time using C++ and I have a link problem.

This is the code...

#include <iostream>
#include "libc++/pcslib.h"

int main()
{
        std::cout << "About to load the server and all the clients." << std::endl;

        Pcs serverProcess("server");
        Pcs clientProcess("client");

        serverProcess.Join();
        clientProcess.Join();

        std::cout << "After joining with processes." << std::endl;                       

        return 0;
}

The included file pcslib.h is:

#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>

class Pcs {
   private:
      int pcsid;
      int status;
   public:
      Pcs();
      Pcs(char *fichier);
      void Fork(char *fichier);
      int Join();
      void Detruit();
};

There's a cpp file, pcsfile.cpp, that implements the public methods. When I'm passing the first block of code to the compiler (g++), I'm having this message and I don't know why. Can you help me?

Undefined first referenced
symbol in file
Pcs::Join() /var/tmp//ccsCx3jl.o
Pcs:: Pcs(char*) /var/tmp//ccsCx3jl.o
ld: fatal: Symbol referencing errors. No output written to loader
collect2: ld returned 1 exit status

It looks like it compiled, so the symbol was defined enough for the compiler to recognize them.

The link process is where the actual executable is built. The linker needs to match up the symbol that your main calls with the actual implementation.

You should probably add the pcsfile.cpp to the g++ command line so that it will compile and the linker will look there for symbol definitions.

There are other ways to do it, but that looks the easiest based on your description.

Thank you for your help. It worked when I added pcslib.cpp with the other file on the command line with g++.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.