Trying to statically link MYSQL library in C++ without any luck - c++

I'm stuck on this issue and I don't think it should be this difficult. I'm essentially getting a compilation error with several 'undefined reference' warnings which are, I believe, caused by the library not being properly linked to the project.
The library resources along with the required header files are all located in the include folder of my project as well as in the include path. (For reference, I'm working inside of VScode).
Here's part of my makefile
MYSQL_CONCPP_DIR = /home/me/Git/CppProject/include/cpp-connector-mysql
UTILS_DIR = /home/me/Git/CppProject/include/utils/
CPPFLAGS = -DSTATIC_CONCPP -I $(UTILS_DIR)
LDLIBS = $(MYSQL_CONCPP_DIR)/lib64/libmysqlcppconn8-static.a -I $(UTILS_DIR) -lssl -lcrypto -lpthread
CXXFLAGS := -std=c++17 -Wall -Wextra -g
I started moving towards C++ development maybe a week ago and have been programming for maybe half a year. So, if there is something obvious that I am missing I would not mind being embarrassed on Stack Overflow, as long as I learn something.
I've scoured the internet to find a solution and nothing has worked. I successfully did the exact same thing I'm trying to do (connect to localhost via a User class and a Connector class) in both Java and Python in a few hours, but am stuck on C++ with this issue.
Here's the actual error message I get, which is incredibly difficult to decipher:
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o: in function `check_lib()':
/home/adis/Git/CppProject/include/cpp-connector-mysql/jdbc/cppconn/driver.h:77: undefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: /home/adis/Git/CppProject/include/cpp-connector-mysql/jdbc/cppconn/driver.h:78: undefined reference to `check(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o: in function `get_driver_instance_by_name':
/home/adis/Git/CppProject/include/cpp-connector-mysql/jdbc/cppconn/driver.h:90: undefined reference to `_get_driver_instance_by_name'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o: in function `mysqlx::abi2::r0::string::traits<char>::to_str[abi:cxx11](mysqlx::abi2::r0::string const&)':
/home/adis/Git/CppProject/include/cpp-connector-mysql/mysqlx/devapi/common.h:225: undefined reference to `mysqlx::abi2::r0::string::Impl::to_utf8[abi:cxx11](mysqlx::abi2::r0::string const&)'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o: in function `mysqlx::abi2::r0::DbDoc::DbDoc()':
/home/adis/Git/CppProject/include/cpp-connector-mysql/mysqlx/devapi/document.h:107: undefined reference to `vtable for mysqlx::abi2::r0::DbDoc'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o: in function `mysqlx::abi2::r0::DbDoc::~DbDoc()':
/home/adis/Git/CppProject/include/cpp-connector-mysql/mysqlx/devapi/document.h:81: undefined reference to `vtable for mysqlx::abi2::r0::DbDoc'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o: in function `mysqlx::abi2::r0::Value::print(std::ostream&) const':
/home/adis/Git/CppProject/include/cpp-connector-mysql/mysqlx/devapi/document.h:508: undefined reference to `mysqlx::abi2::r0::common::Value::print(std::ostream&) const'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o:(.data.rel.ro._ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE[_ZTVN6mysqlx4abi22r05ValueE]+0x18): undefined reference to `typeinfo for mysqlx::abi2::r0::common::Value'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o:(.data.rel.ro._ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE[_ZTVN6mysqlx4abi22r05ValueE]+0x20): undefined reference to `mysqlx::abi2::r0::common::Value::print(std::ostream&) const'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: src/Connector.o:(.data.rel.ro._ZTIN6mysqlx4abi22r05ValueE[_ZTIN6mysqlx4abi22r05ValueE]+0x28): undefined reference to `typeinfo for mysqlx::abi2::r0::common::Value'
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
make: *** [Makefile:76: main] Error 1

Related

Using LTO causes undefined reference to std::basic_string destructor

Decided to try LTO in my build (CentOS 7, CMake 3.14.3, gcc 8.2.1, -std=c++17):
# this adds "-flto -fno-fat-lto-objects"
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
All projects build fine, with exception of one:
/home/user/vcpkg/installed/x64-linux/lib/libazurestorage.a(operation_context.cpp.o): In function `_GLOBAL__sub_I_operation_context.cpp':
operation_context.cpp:(.text.startup+0x9c): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/home/user/vcpkg/installed/x64-linux/lib/libazurestorage.a(cloud_block_blob.cpp.o): In function `_GLOBAL__sub_I_cloud_block_blob.cpp':
cloud_block_blob.cpp:(.text.startup+0x9c): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/home/user/vcpkg/installed/x64-linux/lib/libazurestorage.a(cloud_blob_container.cpp.o): In function `_GLOBAL__sub_I_cloud_blob_container.cpp':
cloud_blob_container.cpp:(.text.startup+0x9c): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/home/user/vcpkg/installed/x64-linux/lib/libazurestorage.a(cloud_blob_shared.cpp.o): In function `_GLOBAL__sub_I_cloud_blob_shared.cpp':
cloud_blob_shared.cpp:(.text.startup+0x9c): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/home/user/vcpkg/installed/x64-linux/lib/libazurestorage.a(cloud_blob_ostreambuf.cpp.o): In function `_GLOBAL__sub_I_cloud_blob_ostreambuf.cpp':
cloud_blob_ostreambuf.cpp:(.text.startup+0x9c): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/home/user/vcpkg/installed/x64-linux/lib/libazurestorage.a(cloud_blob_client.cpp.o):cloud_blob_client.cpp:(.text.startup+0x9c): more undefined references to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()' follow
collect2: error: ld returned 1 exit status
For some mysterious reason libazurestorage.a provided by vcpkg (where it is built without LTO) can't resolve std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (and only that symbol!).
To make it even more interesting -- other projects that use libazurestorage.a build just fine. I've checked linker's cmdline -- they look practically identical (almost same set of libs, almost same order).
Any ideas what could be wrong? Or how to figure out why other projects build fine?

Undefined reference problem with antlr4 c++ target

I am taking a compiler design course and I am trying to learn about ANTLR4 with a C++ target.
Well, that's what I went through:
I Wrote the grammar in a file named "ArrayInit.g4"
Compiled it using ANTLR4 tool and the command "antlr4 -Dlanguage=Cpp -o build" and got a bunch of files in the "build/" directory where I added also a simple file containing the main function int main(){return 0;}
Downloaded the C++ runtime and put it in a directory "runtime/"
In this stage I got stuck, I could not compile the .cpp files using the command "g++ -std=c++11 -I ../runtime -o exec *.cpp" and i get the following error messages:
/tmp/cc8VNGfx.o: In function `ArrayInitLexer::ArrayInitLexer(antlr4::CharStream*)':
ArrayInitLexer.cpp:(.text+0xc8): undefined reference to `antlr4::Lexer::Lexer(antlr4::CharStream*)'
ArrayInitLexer.cpp:(.text+0x116): undefined reference to `antlr4::atn::LexerATNSimulator::LexerATNSimulator(antlr4::Lexer*, antlr4::atn::ATN const&, std::vector<antlr4::dfa::DFA, std::allocator<antlr4::dfa::DFA> >&, std::unordered_set<std::shared_ptr<antlr4::atn::PredictionContext>, antlr4::atn::PredictionContextHasher, antlr4::atn::PredictionContextComparer, std::allocator<std::shared_ptr<antlr4::atn::PredictionContext> > >&)'
/tmp/cc8VNGfx.o: In function `ArrayInitLexer::Initializer::Initializer()':
ArrayInitLexer.cpp:(.text+0x376): undefined reference to `antlr4::dfa::Vocabulary::getLiteralName[abi:cxx11](unsigned long) const'
ArrayInitLexer.cpp:(.text+0x3a6): undefined reference to `antlr4::dfa::Vocabulary::getSymbolicName[abi:cxx11](unsigned long) const'
ArrayInitLexer.cpp:(.text+0x4b2): undefined reference to `antlr4::atn::ATNDeserializer::ATNDeserializer()'
ArrayInitLexer.cpp:(.text+0x4d2): undefined reference to `antlr4::atn::ATNDeserializer::deserialize(std::vector<unsigned short, std::allocator<unsigned short> > const&)'
ArrayInitLexer.cpp:(.text+0x4e8): undefined reference to `antlr4::atn::ATN::operator=(antlr4::atn::ATN&&)'
ArrayInitLexer.cpp:(.text+0x4f7): undefined reference to `antlr4::atn::ATN::~ATN()'
ArrayInitLexer.cpp:(.text+0x503): undefined reference to `antlr4::atn::ATN::getNumberOfDecisions() const'
ArrayInitLexer.cpp:(.text+0x551): undefined reference to `antlr4::atn::ATN::getDecisionState(unsigned long) const'
ArrayInitLexer.cpp:(.text+0x598): undefined reference to `antlr4::atn::ATNDeserializer::~ATNDeserializer()'
ArrayInitLexer.cpp:(.text+0x601): undefined reference to `antlr4::atn::ATNDeserializer::~ATNDeserializer()'
/tmp/cc8VNGfx.o: In function `__static_initialization_and_destruction_0(int, int)':
ArrayInitLexer.cpp:(.text+0x6ec): undefined reference to `antlr4::atn::ATN::ATN()'
ArrayInitLexer.cpp:(.text+0x701): undefined reference to `antlr4::atn::ATN::~ATN()'
ArrayInitLexer.cpp:(.text+0xee6): undefined reference to `antlr4::dfa::Vocabulary::Vocabulary(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)'
ArrayInitLexer.cpp:(.text+0xefb): undefined reference to `antlr4::dfa::Vocabulary::~Vocabulary()'
/tmp/cc8VNGfx.o: In function `antlr4::Lexer::~Lexer()':
ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0xf): undefined reference to `vtable for antlr4::Lexer'
ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x1d): undefined reference to `vtable for antlr4::Lexer'
ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x7d): undefined reference to `antlr4::TokenSource::~TokenSource()'
ArrayInitLexer.cpp:(.text._ZN6antlr45LexerD2Ev[_ZN6antlr45LexerD5Ev]+0x89): undefined reference to `antlr4::Recognizer::~Recognizer()'
/tmp/cc8VNGfx.o: In function `void __gnu_cxx::new_allocator<antlr4::dfa::DFA>::construct<antlr4::dfa::DFA, antlr4::atn::DecisionState*, unsigned long&>(antlr4::dfa::DFA*, antlr4::atn::DecisionState*&&, unsigned long&)':
ArrayInitLexer.cpp:(.text._ZN9__gnu_cxx13new_allocatorIN6antlr43dfa3DFAEE9constructIS3_JPNS1_3atn13DecisionStateERmEEEvPT_DpOT0_[_ZN9__gnu_cxx13new_allocatorIN6antlr43dfa3DFAEE9constructIS3_JPNS1_3atn13DecisionStateERmEEEvPT_DpOT0_]+0x60): undefined reference to `antlr4::dfa::DFA::DFA(antlr4::atn::DecisionState*, unsigned long)'
/tmp/cc8VNGfx.o: In function `void std::_Construct<antlr4::dfa::DFA, antlr4::dfa::DFA>(antlr4::dfa::DFA*, antlr4::dfa::DFA&&)':
ArrayInitLexer.cpp:(.text._ZSt10_ConstructIN6antlr43dfa3DFAEJS2_EEvPT_DpOT0_[_ZSt10_ConstructIN6antlr43dfa3DFAEJS2_EEvPT_DpOT0_]+0x44): undefined reference to `antlr4::dfa::DFA::DFA(antlr4::dfa::DFA&&)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x38): undefined reference to `antlr4::Recognizer::getTokenTypeMap[abi:cxx11]()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x40): undefined reference to `antlr4::Recognizer::getRuleIndexMap[abi:cxx11]()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x48): undefined reference to `antlr4::Recognizer::getTokenType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x60): undefined reference to `antlr4::Recognizer::getErrorHeader[abi:cxx11](antlr4::RecognitionException*)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x68): undefined reference to `antlr4::Recognizer::getTokenErrorDisplay[abi:cxx11](antlr4::Token*)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x70): undefined reference to `antlr4::Recognizer::addErrorListener(antlr4::ANTLRErrorListener*)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x78): undefined reference to `antlr4::Recognizer::removeErrorListener(antlr4::ANTLRErrorListener*)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x80): undefined reference to `antlr4::Recognizer::removeErrorListeners()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x88): undefined reference to `antlr4::Recognizer::getErrorListenerDispatch()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x90): undefined reference to `antlr4::Recognizer::sempred(antlr4::RuleContext*, unsigned long, unsigned long)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0x98): undefined reference to `antlr4::Recognizer::precpred(antlr4::RuleContext*, int)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xa0): undefined reference to `antlr4::Recognizer::action(antlr4::RuleContext*, unsigned long, unsigned long)'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xa8): undefined reference to `antlr4::Recognizer::getState() const'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xb8): undefined reference to `antlr4::Lexer::getInputStream()'
/tmp/cc8VNGfx.o:(.data.rel.ro._ZTV14ArrayInitLexer[_ZTV14ArrayInitLexer]+0xc0): undefined reference to `antlr4::Lexer::setInputStream(antlr4::IntStream*)'
I am totaly new to C++ and I am skipping a basic idea maybe.
Edit1: I am using Linux
Edit2:
When I execute the commande antlr4 -Dlanguage=Cpp -o build I get a bunch of .cpp and .h files as: ArrayInitLexer.h and many of them include antlr4-runtime.h .This header file is present in the runtime/ folder which by it self includes many other header files in the same directory (.i.e runtime/) that contains .h and .cpp files (I see no .a or .so files).
You can find the runtime here under the C++ target section or directly from here
Your compile command doesn't look quite right.
Note that g++ takes an uppercase -L switch to specify a location to look for libraries, and a lowercase -l to specify the filename of a library to link, not including a lib prefix and the file suffix. Since in at least the version I see, antlr provides a "libantlr.a", this would be -l antlr.
Also, the library should be given after your program source or object file(s), because in most cases the linker decides what pieces of libraries to include based on whether they provide any symbols it knows are needed by what it has seen so far.
So I'm not sure if this will solve all the problems, but a step forward would be:
g++ -std=c++11 -Wall -Wextra *.cpp -L ../runtime -l antlr -o exec
I've also taken the liberty of adding the -W flags to enable a default set of compiler warnings, which is a must when writing new C++ code, whether you're new to the language or not.

CppDB compilation errors

I followed the following steps in order to build CppDB:
svn co http://cppcms.svn.sourceforge.net/svnroot/cppcms/cppdb/trunk cppdb-trunk
cd cppdb-trunk
cmake ~/Desktop/cppdb-trunk
make
make install
Afterwards, I'm trying to compile and run the file example1.cpp from the examples directory.
When I run gcc example1.cpp I get the following errors:
/tmp/ccGWenrv.o: In function main':
example1.cpp:(.text+0x2a): undefined reference tostd::allocator::allocator()'
example1.cpp:(.text+0x42): undefined reference to std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
example1.cpp:(.text+0x58): undefined reference tocppdb::session::session(std::__cxx11::basic_string, std::allocator > const&)'
example1.cpp:(.text+0x64): undefined reference to std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
example1.cpp:(.text+0x73): undefined reference tostd::allocator::~allocator()'
example1.cpp:(.text+0x91): undefined reference to cppdb::session::operator<<(char const*)'
example1.cpp:(.text+0xa5): undefined reference tocppdb::statement::operator<<(void ()(cppdb::statement&))'
example1.cpp:(.text+0xb4): undefined reference to cppdb::statement::~statement()'
example1.cpp:(.text+0xd2): undefined reference tocppdb::session::operator<<(char const)'
example1.cpp:(.text+0xe6): undefined reference to cppdb::statement::operator<<(void (*)(cppdb::statement&))'
example1.cpp:(.text+0xf5): undefined reference tocppdb::statement::~statement()'
example1.cpp:(.text+0x170): undefined reference to cppdb::statement::statement()'
example1.cpp:(.text+0x18e): undefined reference tocppdb::session::operator<<(char const*)'
example1.cpp:(.text+0x1db): undefined reference to cppdb::statement::operator<<(tm const&)'
example1.cpp:(.text+0x1e8): undefined reference tocppdb::statement::operator<<(char const*)'
example1.cpp:(.text+0x1fd): undefined reference to `cppdb::statement::operator=(cppdb::statement const&)'
etc.
Any suggestions?
Try g++ example1.cpp -lcppdb. You need to link against the C++ run-time library (using g++ instead of gcc takes care of that) and against CppDB itself (the -lcppdb part). If GCC cannot find the -lcppdb library, you'll have to provide the path using -L, and you may have to use LD_LIBRARY_PATH at run time as well.

Understanding make error message

So, I cloned SqAtx's GitHub repository SuperMarioWorld onto my Ubuntu 16.04 (64bit) machine. I would like to run his Super Mario clone in order to understand his project and learn by the way he did this game.
First of all, I could not compile it as he explained it in the README.md. However, I have successfully compiled an own Battleship game the same way (which tells me Cmake, make, SFML, and a C compiler are correctly installed). As an error I got this error message after running cmake .. from the build folder:
CMake Error at CMakeLists.txt:24 (add_executable):
add_executable called with incorrect number of arguments
-- Found SFML 2.4.0 in /usr/include
CMake Error at CMakeLists.txt:32 (target_link_libraries):
Cannot specify link libraries for target "SuperMarioWorld" which is not
built by this project.
-- Configuring incomplete, errors occurred!
I then modified his CMakeList.txt so that it successfully creates a makefile. My CMakeList.txt looks as follows:
#Change this if you need to target a specific CMake version
cmake_minimum_required (VERSION 2.6)
# Enable debug symbols by default
# must be done before project() statement
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)" FORCE)
endif()
project (SuperMarioWorld)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -Wall -g")
# I guess you have not released the project yet :p
set (SuperMarioWorld_VERSION_MAJOR 0)
set (SuperMarioWorld_VERSION_MINOR 1)
set (SuperMarioWorld_VERSION_PATCH 0)
include_directories("${PROJECT_BINARY_DIR}")
set(SOURCE_FILES
EventEngine/Listeners/CharacterDiedListener.cpp
EventEngine/Listeners/CharacterPositionUpdateListener.cpp
EventEngine/Listeners/CloseRequestListener.cpp
EventEngine/Listeners/DebugInfoUpdatedListener.cpp
EventEngine/Listeners/ForegroundItemRemovedListener.cpp
EventEngine/Listeners/ForegroundItemUpdatedListener.cpp
EventEngine/Listeners/GotLevelInfoListener.cpp
EventEngine/Listeners/KeyboardListener.cpp
EventEngine/Listeners/LevelStartListener.cpp
EventEngine/Listeners/MarioJumpListener.cpp
EventEngine/Listeners/MarioKickedEnemyListener.cpp
EventEngine/Listeners/NewCharacterReadListener.cpp
EventEngine/Listeners/NewForegroundItemReadListener.cpp
EventEngine/Listeners/NewPipeReadListener.cpp
EventEngine/Listeners/ToggleIgnoreInputListener.cpp
EventEngine/EventEngine.cpp
Game/CollisionHandler.cpp
Game/GameEngine.cpp
Game/LevelImporter.cpp
Graphics/GraphicsEngine.cpp
Graphics/SpriteHandler.cpp
Sound/SoundEngine.cpp
SuperMario/Game.cpp
SuperMario/main.cpp
System/Characters/Enemy.cpp
System/Characters/Goomba.cpp
System/Characters/MovingObject.cpp
System/Characters/Player.cpp
System/Items/Box.cpp
System/Items/Pipe.cpp
System/irrXML/irrXML.cpp
System/Engine.cpp
System/Util.cpp
)
# Define sources and executable
set (EXECUTABLE_NAME "SuperMarioWorld")
add_executable(${EXECUTABLE_NAME} ${SOURCE_FILES})
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/lib" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
# Install target
install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)
With this CMakeList.txt I could successfully create a makefile. Running the make I first got two errors which where the same:
/home/lex/Documents/cs/games/SuperMarioWorld/EventEngine/Listeners/NewForegroundItemReadListener.cpp:21:95: error: taking address of temporary [-fpermissive]
m_graphicsEngine->UpdateForegroundItem(&(_event->GetDisplayableObject()->GetInfoForDisplay()));
So I had to fix it in NewPipeReadListener.cpp on line 24 and in NewForegroundItemReadListener.cpp on line 24. I fixed it like this:
InfoForDisplay temp = _event->GetPipe()->GetInfoForDisplay();
m_graphicsEngine->UpdateForegroundItem(&temp);
Now, the makefile gives me an error I cannot fix, since I don't understand a word.. I would like to include the whole error message here, but Stackoverflow doesn't allow me to do so...
But it starts like this.
[ 2%] Linking CXX executable SuperMarioWorld
CMakeFiles/SuperMarioWorld.dir/EventEngine/Listeners/KeyboardListener.cpp.o: In function `KeyboardListener::onEvent(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Event*)':
/home/lex/Documents/cs/games/SuperMarioWorld/EventEngine/Listeners/KeyboardListener.cpp:12: undefined reference to `KeyboardEvent::GetType()'
/home/lex/Documents/cs/games/SuperMarioWorld/EventEngine/Listeners/KeyboardListener.cpp:13: undefined reference to `KeyboardEvent::GetKey()'
/home/lex/Documents/cs/games/SuperMarioWorld/EventEngine/Listeners/KeyboardListener.cpp:14: undefined reference to `KeyboardEvent::GetType()'
/home/lex/Documents/cs/games/SuperMarioWorld/EventEngine/Listeners/KeyboardListener.cpp:15: undefined reference to `KeyboardEvent::GetKey()'
CMakeFiles/SuperMarioWorld.dir/Game/CollisionHandler.cpp.o: In function `CollisionHandler::HandleCollisionsWithMapEdges(MovingObject&)':
/home/lex/Documents/cs/games/SuperMarioWorld/Game/CollisionHandler.cpp:25: undefined reference to `DisplayableObject::GetCoordinates() const'
CMakeFiles/SuperMarioWorld.dir/Game/CollisionHandler.cpp.o: In function `CollisionHandler::DetectCollisionWithObj(MovingObject&, DisplayableObject&)':
I would appreciate if someone could help me understand this error and possibly make the project run on my system.
EDIT:
After fixing the the first error which was including EventEngine/KeyboardEvent.cpp it links up to a 100% and spits out the following error message:
[100%] Linking CXX executable SuperMarioWorld
CMakeFiles/SuperMarioWorld.dir/Game/CollisionHandler.cpp.o: In function `CollisionHandler::HandleCollisionsWithMapEdges(MovingObject&)':
/home/lex/Documents/cs/games/SuperMarioWorld/Game/CollisionHandler.cpp:25: undefined reference to `DisplayableObject::GetCoordinates() const'
CMakeFiles/SuperMarioWorld.dir/Game/CollisionHandler.cpp.o: In function `CollisionHandler::DetectCollisionWithObj(MovingObject&, DisplayableObject&)':
/home/lex/Documents/cs/games/SuperMarioWorld/Game/CollisionHandler.cpp:40: undefined reference to `DisplayableObject::GetCoordinates() const'
/home/lex/Documents/cs/games/SuperMarioWorld/Game/CollisionHandler.cpp:40: undefined reference to `DisplayableObject::GetCoordinates() const'
CMakeFiles/SuperMarioWorld.dir/Game/CollisionHandler.cpp.o: In function `CollisionHandler::ReactToCollisionsWithObj(MovingObject&, DisplayableObject&, CollisionDirection)':
/home/lex/Documents/cs/games/SuperMarioWorld/Game/CollisionHandler.cpp:45: undefined reference to `DisplayableObject::GetCoordinates() const'
CMakeFiles/SuperMarioWorld.dir/Game/CollisionHandler.cpp.o: In function `CollisionHandler::HandleCollisionWithRect(unsigned int, sf::Rect<float>)':
/home/lex/Documents/cs/games/SuperMarioWorld/Game/CollisionHandler.cpp:73: undefined reference to `DisplayableObject::GetCoordinates() const'
CMakeFiles/SuperMarioWorld.dir/Game/CollisionHandler.cpp.o:/home/lex/Documents/cs/games/SuperMarioWorld/Game/CollisionHandler.cpp:138: more undefined references to `DisplayableObject::GetCoordinates() const' follow
CMakeFiles/SuperMarioWorld.dir/Game/GameEngine.cpp.o: In function `GameEngine::UpdateForegroundItem(unsigned int, sf::Rect<float>)':
/home/lex/Documents/cs/games/SuperMarioWorld/Game/GameEngine.cpp:114: undefined reference to `DisplayableObject::SetCoordinates(sf::Rect<float>)'
CMakeFiles/SuperMarioWorld.dir/Game/LevelImporter.cpp.o: In function `LevelImporter::StoreFloor()':
/home/lex/Documents/cs/games/SuperMarioWorld/Game/LevelImporter.cpp:180: undefined reference to `DisplayableObject::DisplayableObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, sf::Vector2<float>, State)'
CMakeFiles/SuperMarioWorld.dir/System/Characters/MovingObject.cpp.o: In function `MovingObject::MovingObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, sf::Vector2<float>, State)':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Characters/MovingObject.cpp:5: undefined reference to `DisplayableObject::DisplayableObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, sf::Vector2<float>, State)'
/home/lex/Documents/cs/games/SuperMarioWorld/System/Characters/MovingObject.cpp:5: undefined reference to `DisplayableObject::~DisplayableObject()'
CMakeFiles/SuperMarioWorld.dir/System/Characters/MovingObject.cpp.o: In function `MovingObject::MovingObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, float, float, State)':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Characters/MovingObject.cpp:10: undefined reference to `DisplayableObject::DisplayableObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, float, float, State)'
/home/lex/Documents/cs/games/SuperMarioWorld/System/Characters/MovingObject.cpp:10: undefined reference to `DisplayableObject::~DisplayableObject()'
CMakeFiles/SuperMarioWorld.dir/System/Characters/MovingObject.cpp.o: In function `MovingObject::~MovingObject()':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Characters/MovingObject.cpp:25: undefined reference to `DisplayableObject::~DisplayableObject()'
CMakeFiles/SuperMarioWorld.dir/System/Characters/MovingObject.cpp.o: In function `MovingObject::GetInfoForDisplay()':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Characters/MovingObject.cpp:32: undefined reference to `DisplayableObject::GetInfoForDisplay()'
CMakeFiles/SuperMarioWorld.dir/System/Characters/MovingObject.cpp.o:(.rodata._ZTI12MovingObject[_ZTI12MovingObject]+0x10): undefined reference to `typeinfo for DisplayableObject'
CMakeFiles/SuperMarioWorld.dir/System/Items/Box.cpp.o: In function `Box::Box(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, sf::Vector2<float>, State)':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Box.cpp:3: undefined reference to `DisplayableObject::DisplayableObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, sf::Vector2<float>, State)'
CMakeFiles/SuperMarioWorld.dir/System/Items/Box.cpp.o: In function `Box::Box(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, float, float, State)':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Box.cpp:8: undefined reference to `DisplayableObject::DisplayableObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, float, float, State)'
CMakeFiles/SuperMarioWorld.dir/System/Items/Box.cpp.o:(.rodata._ZTV3Box[_ZTV3Box]+0x20): undefined reference to `DisplayableObject::GetInfoForDisplay()'
CMakeFiles/SuperMarioWorld.dir/System/Items/Box.cpp.o: In function `Box::~Box()':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Box.hpp:9: undefined reference to `DisplayableObject::~DisplayableObject()'
CMakeFiles/SuperMarioWorld.dir/System/Items/Box.cpp.o:(.rodata._ZTI3Box[_ZTI3Box]+0x10): undefined reference to `typeinfo for DisplayableObject'
CMakeFiles/SuperMarioWorld.dir/System/Items/Pipe.cpp.o: In function `Pipe::Pipe(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, sf::Vector2<float>, int, PipeType, EventEngine*)':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Pipe.cpp:5: undefined reference to `DisplayableObject::DisplayableObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, sf::Vector2<float>, State)'
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Pipe.cpp:5: undefined reference to `DisplayableObject::~DisplayableObject()'
CMakeFiles/SuperMarioWorld.dir/System/Items/Pipe.cpp.o: In function `Pipe::~Pipe()':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Pipe.cpp:14: undefined reference to `DisplayableObject::~DisplayableObject()'
CMakeFiles/SuperMarioWorld.dir/System/Items/Pipe.cpp.o: In function `Pipe::SpawnEnemyIfTimeElapsed()':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Pipe.cpp:50: undefined reference to `DisplayableObject::DisplayableObject(EventEngine*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, float, float, State)'
CMakeFiles/SuperMarioWorld.dir/System/Items/Pipe.cpp.o: In function `Pipe::MoveEnemyBeingSpawned(float)':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Pipe.cpp:58: undefined reference to `DisplayableObject::Slide(float, float)'
CMakeFiles/SuperMarioWorld.dir/System/Items/Pipe.cpp.o: In function `Pipe::IsEnemyReadyToLeavePipe()':
/home/lex/Documents/cs/games/SuperMarioWorld/System/Items/Pipe.cpp:92: undefined reference to `DisplayableObject::GetCoordinates() const'
CMakeFiles/SuperMarioWorld.dir/System/Items/Pipe.cpp.o:(.rodata._ZTV4Pipe[_ZTV4Pipe]+0x20): undefined reference to `DisplayableObject::GetInfoForDisplay()'
CMakeFiles/SuperMarioWorld.dir/System/Items/Pipe.cpp.o:(.rodata._ZTV4Pipe[_ZTV4Pipe]+0x28): undefined reference to `DisplayableObject::UpdateAfterCollision(CollisionDirection, ObjectClass)'
CMakeFiles/SuperMarioWorld.dir/System/Items/Pipe.cpp.o:(.rodata._ZTI4Pipe[_ZTI4Pipe]+0x10): undefined reference to `typeinfo for DisplayableObject'
collect2: error: ld returned 1 exit status
CMakeFiles/SuperMarioWorld.dir/build.make:957: recipe for target 'SuperMarioWorld' failed
make[2]: *** [SuperMarioWorld] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/SuperMarioWorld.dir/all' failed
make[1]: *** [CMakeFiles/SuperMarioWorld.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
EDIT2:
Now, I feel stupid, there was another file I forgot to include, it was System/DisplayableObject.cpp.
Thanks, everyone for the help!!!
You did not include all the source files in the build.
For instance, in your error message the linker is complaining that it is missing the definition for the KeyboardEvent::GetType() function.
Searching the repo on github for KeyboardEvent will quickly tell you that this function is defined in EventEngine/KeyboardEvent.cpp, which is not part of your CMake's SOURCE_FILES.
You might be missing other source files as well. Try fixing the linker errors one by one until it compiles.

C++ linker errors, Undefined references to Items that should be referenced from a Share Object

I am building a library, called physgameengine, to aid in building games. This game library links against several libraries, including: Ogre3d, Bullet Physics and SDL, which are in the library files libOgreMain-1.6.5.so, libBulletCollision.a, libBulletDynamics.a, libBulletSoftBody.a, libSDL-1.2.so.0. This Library/Shared object appears to build just fine in Ubuntu 9.10, using GCC and Code::blocks. It does build fine on windows with mingw and Code::blocks.
In windows I can make games that compile/link using the library I built just fine, those programs run and do exactly what I expect it to. In Ubuntu when I try to compile/link a game that uses this Library I am making I get lots of undefined reference errors. But none to the functions or objects in the library I built, but rather to the items that my library calls. Here is an example of the errors I get.
> ||warning: libOgreMain-1.6.5.so, needed by ../physgameengine/bin/ubuntudebug/libphysgame.so, not found (try using -rpath or -rpath-link)|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Root::Root(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::StringUtil::BLANK'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionWorld::updateAabbs()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Root::createRenderWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int, bool, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const*)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Timer::getMilliseconds()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Root::createSceneManager(unsigned short, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Root::~Root()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionWorld::~btCollisionWorld()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Root::renderOneFrame()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionObject::btCollisionObject()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Timer::getMillisecondsCPU()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btSimulationIslandManager::buildAndProcessIslands(btDispatcher*, btCollisionWorld*, btSimulationIslandManager::IslandCallback*)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Viewport::getActualWidth() const'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Root::initialise(bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Camera::lookAt(Ogre::Vector3 const&)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionWorld::removeCollisionObject(btCollisionObject*)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Viewport::getActualHeight() const'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Timer::reset()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Root::restoreConfig()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Timer::~Timer()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::NedAllocImpl::deallocBytes(void*)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionWorld::convexSweepTest(btConvexShape const*, btTransform const&, btTransform const&, btCollisionWorld::ConvexResultCallback&, float) const'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::NedAllocImpl::allocBytes(unsigned int, char const*, int, char const*)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `typeinfo for btCollisionWorld'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionObject::~btCollisionObject()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btSimulationIslandManager::btSimulationIslandManager()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Root::showConfigDialog()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionObject::setActivationState(int)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::LogManager::getSingleton()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionWorld::performDiscreteCollisionDetection()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::LogManager::logMessage(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Ogre::LogMessageLevel, bool)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Timer::Timer()'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionWorld::btCollisionWorld(btDispatcher*, btBroadphaseInterface*, btCollisionConfiguration*)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `typeinfo for btCollisionObject'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `Ogre::Camera::setPosition(Ogre::Vector3 const&)'|
../physgameengine/bin/ubuntudebug/libphysgame.so||undefined reference to `btCollisionWorld::addCollisionObject(btCollisionObject*, short, short)'|
||=== Build finished: 36 errors, 1 warnings ===|
The bulk of the code is identical between windows and Linux they seem to have equivalent compile/linker settings. But the Linux game appears to be linking against the original libraries that my library should be linking against. What is going on? Any Ideas on how to fix it?
Unlike in Windows, in Linux systems it's not enough to place a shared library file into the same folder as executable.
You should either provide an LD_LIBRARY_PATH variable or follow the linker's advice and use -rpath (check this question)
The very first line of your error tells you the problem:
libOgreMain-1.6.5.so, needed by libphysgame.so, not found (try using -rpath or -rpath-link)|
You need to specify the directory where libOgreMain is installed. Since you need to use a linker option, you need to use the -Wl feature of gcc. Assuming the files is in /opt/lib, you would add:
gcc -Wl,-rpath-link,/opt/lib ...rest of linker line...
Just based on the first line:
> ||warning: libOgreMain-1.6.5.so, needed by ../physgameengine/bin/ubuntudebug/libphysgame.so, not found (try using -rpath or -rpath-link)|
It looks like your linker is having trouble finding your shared object. Check to make sure your linker's include paths are properly set up.
This is a good link on the subject http://sysprogs.com/w/fixing-rpath-link-issues-with-cross-compilers/
Essentially if you do not have -lmylib for all your libraries you link to, you cannot count on -Lpath to find them. Thus, if /opt/infoa/lib/liba.so depends on /opt/infod/lib/libd.so and you only specify -la, then you need something like this:
gcc -Wl,-rpath-link,/opt/infod/lib -L/opt/infoa/lib -la *.c -o myexec