Problems compiling windows c++ project on g++ 5 [duplicate] - c++

This question already has answers here:
Using G++ to compile multiple .cpp and .h files
(13 answers)
Closed 7 years ago.
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...

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>

Related

Xcode 8 - c++ - Undefined symbols for architecture x86_64: "liblas::Reader....."

I am currently working on a c++ project using xcode 8, and I keep getting this error:
Undefined symbols for architecture x86_64:
"liblas::Reader::Reader(std::__1::basic_istream<char, std::__1::char_traits<char> >&)", referenced from:
_main in main.o
"liblas::Reader::~Reader()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have compiled boost, liblas, etc. I have it included in my cmakelists.txt as well, and have the folder it is included in listed in 'Framework Search Paths' as well as 'Header Search Paths'. My compiler is the xcode 8 default of Apple LLVM 8.0. My deployment target is 10.11. The valid architectures are 'x86_64' and 'i386'.
Here is the code that attempts to use libLAS (file.las contains a hardcoded path to my las file). This code was pulled directly off the libLAS c++ tutorial here:
std::ifstream ifs;
ifs.open("file.las", std::ios::in | std::ios::binary);
liblas::ReaderFactory f;
liblas::Reader reader = f.CreateWithStream(ifs);
If I comment out the line liblas::Reader reader = f.CreateWithStream(ifs);, the code compiles fine, and produces a valid executable. With this line here, it throws the error. I need to be able to read LAS files with this project however, so I need to try to get the Reader to work.
So my questions are:
Is there a better way to debug this?
Is this an obvious error that I'm missing?
Is this an issue with the library? (libLAS)
What can I do to get this working?
Thanks to anyone in advance!

linking Boost library to Xcode project

I'm trying to create a project that uses the Boost library. I'm on OS X 10.9.5 (I should update that) and using Xcode 6.2. I installed boost with homebrew brew install boost and it's located in /usr/local/Cellar/boost/1.59.0. I added the path the the /usr/local/Cellar/boost/1.59.0/include to the header search path in Xcode and it seems to recognize it because the autocomplete hinting works.
In the boost documentation it mentions that some of the Boost libraries must be built before they can be used. I assume homebrew took care of that because I have a bunch of .a and .dylib files in /usr/local/Cellar/boost/1.59.0/lib
I'm still new to C++ and the Xcode build process but it seems I still need to link the compiled libraries to my project. I tried adding the path /usr/local/Cellar/boost/1.59.0/lib to my project's library search paths but I'm not sure if that is correct.
Here is the error I get when I try to build my project.
Undefined symbols for architecture x86_64:
"boost::filesystem::detail::create_directory(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::create_directory(boost::filesystem::path const&) in main.o
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::exists(boost::filesystem::path const&) in main.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init2 in main.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.o
___cxx_global_var_init1 in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is there something else I need to configure to get this to work?
Adding the path is correct but you also need to specify the libraries you need. On the commandline you would use -l for that in Xcode you can add them to Other Linker Flags.
The libraries you need are boost_filesystem and boost_system.

Finding glfw library file in xcode in OSX?

So I'm trying to build a program using OpenGL and GLFW. The only errors I'm getting are:
Undefined symbols for architecture x86_64:
"_glewInit", referenced from:
_main in main.o
"_glfwCreateWindow", referenced from:
_main in main.o
"_glfwGetKey", referenced from:
_main in main.o
"_glfwInit", referenced from:
_main in main.o
"_glfwMakeContextCurrent", referenced from:
_main in main.o
"_glfwPollEvents", referenced from:
_main in main.o
"_glfwSetInputMode", referenced from:
_main in main.o
"_glfwSwapBuffers", referenced from:
_main in main.o
"_glfwTerminate", referenced from:
_main in main.o
"_glfwWindowHint", referenced from:
_main in main.o
"_glfwWindowShouldClose", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So obviously this is a linking error. After doing some research, I think I found my error. It seems like I need to add the GLFW library (libglfw.dylib) and the Cocoa and OpenGL libraries. My problem is that I can't find libglfw.dylib - or anything glfw for that matter - under the "Link Binary With Libraries" section. I found OpenGL.framework and Cocoa.framework, but I can't find the glfw one. I tried going through the whole cmake process (cmake > make > sudo make install) for glfw but it didn't show up. I then tried installing using brew and that didn't work either. I can't figure out why I can't find this file. Any ideas? This is my first time doing anything with GLFW and one of my first projects doing any coding on OSX. If I'm forgetting any information you need to help let me know and I'll get it to you all as fast as I can.
First of all, using GLEW on OS X is 100% pointless (OpenGL extensions are all handled statically at link-time on this platform). You should add some code to your project that does something along the lines of:
#ifndef __APPLE__
// GLEW-specific code here
#endif
Also, .dylib is for dynamically linking - there's not much point in doing that on OS X. GLFW is not distributed in the form of a Framework, you'll have to compile it yourself. You can still use a dynamic linking version of GLFW if you wish, but you'd wind up having to package the library in your .app bundle. I would advise you just use the static version and avoid all of that.
Have you reviewed the OS X section of the GLFW compilation instructions?

libFLAC symbols not found and vtable errors

I have compiled libFLAC from source code under macintosh os x 10.6.8 and tested WAV to FLAC conversion with success (from the command line). I have also used the default build as such: ./configure --prefix="$base/flac/more/" --exec-prefix="$base/flac/main/" --disable-asm-optimizations && make && make install
Now after adding the paths for FLAC headers and the libs into a brand new project. I am attempting to decode and load FLAC audio into system memory using libFLAC, under the example code which was bundled with libFLAC. However i am getting symbols not found errors with Standard C and vtable symbols not found errors with the Standard C++ approach (as seen below).
Errors under the C approach:
"FLAC_stream_decoder_get_state", referenced from:
_main in main.o
"FLAC_stream_decoder_new", referenced from:
_main in main.o
"FLAC_StreamDecoderStateString", referenced from:
_main in main.o
"FLAC_StreamDecoderErrorStatusString", referenced from:
error_callback(FLAC__StreamDecoder const*,
FLAC__StreamDecoderErrorStatus, void*)in main.o
"FLAC_stream_decoder_init_file", referenced from:
_main in main.o
"FLAC_StreamDecoderInitStatusString", referenced from:
_main in main.o
"FLAC_stream_decoder_set_md5_checking", referenced from:
_main in main.o
"FLAC_stream_decoder_process_until_end_of_stream", referenced from:
_main in main.o
"FLAC_stream_decoder_delete", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
From what i can understand is either: the linker is not seeing the libs (because it is seeing the headers) or the libFLAC was not compiled correctly. I'll be thankful if anyone will point me to possible solutions given these errors.
I was doing the linking incorrectly. Linking needs to be done according to this stackoverflow post Adding static library inside Xcode C++ project (How to add static libraries inside a C++ project with Xcode) but i was adding the linking directory under the Project Setting where i was adding my headers path, thus it was not working.

Compile g++ on XCode 5?

I'm starting with OpenGL in c++, and I included GLUT and OpenGL frameworks on my project on XCode. When I try to compile and run my program as if it was a simple c++ program it gives me this error:
Undefined symbols for architecture x86_64:
"_glBegin", referenced from:
display() in main.o
"_glClear", referenced from:
display() in main.o
"_glClearColor", referenced from:
etc.
Using a simple c++ program, I don't get any of this errors. I searched the web and I found that I need to compile with g++ (specific for c++ lenguage), but I don't know how to do it. Any help?
You need to add -framework OpenGL to your compiler settings.