2

I am trying to create and link a static library in C++. I have two files:
helloWorld.cpp

#include <iostream>

  int main(int argc, char const *argv[]) {
  std::cout << "I'm inside main." << '\n';
  printHello();
  return 0;
}

libraryHello.cpp

#include <iostream>

void printHello() {
  std::cout << "Hello" << '\n';
}

I am running such commands (based on http://www.techytalk.info/c-cplusplus-library-programming-on-linux-part-one-static-libraries/):

g++ -Wall -c libraryHello.cpp -o libraryHello.o
ar rcs libmylib.a libraryHello.o
g++ -static helloWorld.cpp -L. -lmylib -o helloExecute

First two go well, when trying to compile main file such error appears:

helloWorld.cpp: In function ‘int main(int, const char**)’:
helloWorld.cpp:5:14: error: ‘printHello’ was not declared in this scope

It looks as though it is not loaded at all and it cannot find printHello. Is there anything I am doing wrong when compiling, linking or anything else? What I want to do is to call printHello() procedure from main using static library and linking it to helloWorld.cpp.

My compiler: g++ 5.4.0, OS: Ubuntu 16.04 32bit

3 Answers 3

3

You did not declare the function before use:

#include <iostream>

void printHello(); /// <-- this was missing    

int main(int argc, char const *argv[]) {
  std::cout << "I'm inside main." << '\n';
  printHello();
  return 0;
}

You have everything in place that is needed for linking, but not for compiling. The general idea is like this: The compiler needs a declaration for every function that you use. That is, it has to know its name and signature.

The compiler will now check whether a function call is valid for the given signature and leave a placeholder for the function call. It is up to the linker to resolve those placeholders and replace them with the actual address of the called function.

The linker therefore has to find a matching definition for that function, that is, an actual implementation. If you just declare the function, but forget to define it, compilation will happily succeed, but the linker will complain about an unresolved reference.

This split allows you to compile different source files in isolation: Each source file needs to know the declarations of every function that it uses, but not the definitions. This is enough for the compiler to ensure that a caller uses the function correctly. Declarations are typically placed in header files, to ensure that implementation and caller have a consistent understanding about what the function signature is, even if they reside in distinct source files.

2

helloWorld.cpp

#include <iostream>
#include <libraryHello.h>
using namespace std;

int main(int argc, char const *argv[])
{
    cout << "I'm inside main." << '\n';
    printHello();
    return 0;
}

libraryHello.h (You need add this file)

#ifndef LIBRARYHELLO_H
#define LIBRARYHELLO_H

void printHello();

#endif // LIBRARYHELLO_H

libraryHello.cpp

#include <iostream>
#include <libraryHello.h>
void printHello() {
    std::cout << "Hello" << '\n';
}
0

You still need to have a forward declaration for

 void printHello();

before main().

Otherwise the compiler doesn't know how that function should be called.

The usual way is to put that into a corresponding header file, and include that in the other translation units.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.