0

I have trouble with my C++ project which I created and compiled successfully within Visual Studio 2013 on a Windows machine. Currently I'm migrating the source to Unix platforms (my issue had been confirmed on Mac and CentOs). My project depends on the OpenMP (multithread support) library. Hence Apples LLVM compiler (version 6.0) does not support OpenMp sufficiently I decided to compile my project using g++-5.

After handling some syntax errors when moving from windows compiler to GNU's gcc/g++ (Homebrew gcc 5.2.0) I'm experiencing the following issue in the linking process:

The linker can not find symbols for my architecture and lists nearly every class/object which is created and included by me - what's curious, it does not list every of my classes/object.

I'm compiling with this command:

g++-5 myProject.cpp -o myProject -fopenmp -std=c++11 -L/usr/local/lib -I/usr/local/Cellar/libiomp/20150701/include/libiomp -Wall

Am I missing something stupid in my compiler call? Whats the reason for my linker issues? As mentioned above, the same issue occurs on Mac and CentOs too.

EDIT (according to some comments): According to this question I've activated all warning flags and the compiler is definitely happy with my code. But the linker is saying the following:

  Undefined symbols for architecture x86_64:
  "FileHelper::createFileName[abi:cxx11](char const*, char const*)", referenced from:
      _main in ccZAvFqT.o
  "Statistics::writeStats(char const*)", referenced from:
      _main in ccZAvFqT.o
  "Statistics::newSimulationRun()", referenced from:
      _main in ccZAvFqT.o
  "Statistics::newSimulationIteration()", referenced from:
      _main in ccZAvFqT.o
  "Statistics::Instance()", referenced from:
      _main in ccZAvFqT.o
  "Statistics::writeAvg()", referenced from:
      _main in ccZAvFqT.o
  "Statistics::~Statistics()", referenced from:
      _main in ccZAvFqT.o
  "GraphHelper::Graph::writeGraph(std::vector<IPeer*, std::allocator<IPeer*> >*, std::vector<Connection*, std::allocator<Connection*> >*)", referenced from:
[...] and so on and so on...
Community
  • 1
  • 1
Marschal
  • 173
  • 2
  • 11
  • I don't see a path for finding the OpenMP libraries, some thing like `-L/usr/local/Cellar...`. – R Sahu Nov 24 '15 at 20:54
  • is `myProject.cpp` your only source file? – M.M Nov 24 '15 at 20:56
  • @RSahu `-fopenmp` does the magic on mac. Or `-libgomp` on CentOs. – Marschal Nov 24 '15 at 20:57
  • @M.M `myProject.cpp` ist my main file. It includes lots of others. – Marschal Nov 24 '15 at 20:59
  • Normally you would use `-l` switches to specify the libraries to link. You didn't specify any `-l` switches so it's not really surprising you get link errors. It would help if you post the actual errors (start at the top) – M.M Nov 24 '15 at 20:59
  • @Marschal `myProject.cpp` does `#include "otherfiles.cpp"` you mean ?? – M.M Nov 24 '15 at 20:59
  • @M.M No, sorry for the confusion. I'm never including `.cpp`files. Im including header files and using the classes defined there. – Marschal Nov 24 '15 at 21:01
  • 1
    @Marschal so you actually aren't compiling the other `.cpp` files - that would explain your problem – M.M Nov 24 '15 at 21:01
  • @M.M Could you elaborate a little bit more on it please. I'm including all the header files successively. GNU compiles compiles every cpp file when it's header was included. Hence i could build a includion with my main file in the root, gcc should have compile every cpp file in my project. It is creating the `*.o` files. – Marschal Nov 24 '15 at 21:05
  • @Marschal do you have more than one `.cpp` file in your project? Update your question to indicate whether or not you do. – M.M Nov 24 '15 at 21:07
  • I don't know what you mean by "GNU compiles compiles every cpp file when it's header was included." . Normally projects contain several `.cpp` files. Each of these is compiled separately , creating object files, and then the object files and libraries are linked together. You would have noticed in VS2013 that your project contained multiple .cpp files listed in the project explorer or whatever it's called, and the build process compiled them one by one. With g++ you should do the same thing, although there are two options [cont.] – M.M Nov 24 '15 at 21:09
  • You can either write `g++ file1.cpp file2.cpp file3.cpp -o myexe ...compile-flags... ....link-flags...`, or you can write for each .cpp file `g++ -c file1.cpp -o file1.o ...compile-flags...` and then after all that, `g++ file1.o file2.o file3.o -o myexe ...link-flags...` . The latter approach is more flexible and normally you'd automate the process with a Makefile or some other automated build system – M.M Nov 24 '15 at 21:10
  • I've edited my question by some more detailed information. – Marschal Nov 24 '15 at 21:12
  • @Marschal see my later comments – M.M Nov 24 '15 at 21:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96078/discussion-between-marschal-and-m-m). – Marschal Nov 24 '15 at 21:20

1 Answers1

0

When you are compiling from command line, you need to make sure you include all object files (.o) when you link the executable.

Obviously, it is hard to control this manually, this is why people invented various sorts of Makefiles to do this for them. You can research this topic. To help your immediate problem, just make sure you have all your cpp files listed in your command line for compilation. Something like

g++ myProject.cpp Statistics.cpp ... <flags>  -o <executable>
SergeyA
  • 61,605
  • 5
  • 78
  • 137