Linking error using CMake to create a shared library in C++ because multiple definition of symbol -
i'm having problems compile code, links poco c++ libraries, create shared library linux (mint 13, in case). reduced environment.
i have these 3 files:
il_notify.h
#ifndef __il_notify_h__ #define __il_notify_h__ #include <string> #include "poco/logger.h" #include "poco/logstream.h" using poco::logger; using poco::logstream; namespace mynamespace { // logger. inherits root channel logstream lstr(logger::get("mylogger")); // other class, call logger way: //lstr << "this test" << std::endl; } #endif
il_class1.cpp
#include "il_class1.h" #include "il_notify.h" namespace mynamespace { void il_class1::foo() { // stuff... lstr << "this test msg il_class1::foo" << std::endl; } }
il_class2.cpp
#include "il_class2.h" #include "il_notify.h" namespace mynamespace { void il_class2::bar() { // stuff... lstr << "this test msg il_class2::bar" << std::endl; } }
il_class1
, il_class2
declared @ il_class1.h
, il_class2.h
. no inclussion of il_notify.h
inside headers.
and cmakelists.txt file
# set search path find pococonfig.cmake set(cmake_prefix_path ${cmake_prefix_path} ${cmake_source_dir}/cmake/modules/) # needed packages find_package(poco required) # now, can use poco include_directories( ${poco_include_dirs} ${cmake_current_source_dir}/include ) # libraries linking against link_directories( ${poco_library_dirs} ) # cpp files build library file(glob_recurse all_sources ${cmake_current_source_dir}/src/il_class1.cpp ${cmake_current_source_dir}/src/il_class2.cpp ) # library creation add_library(mylib shared ${all_sources})
i can execute cmake
successfully, create makefile
. when run make
, i error
cmakefiles/mylib.dir/src/il_class2.cpp.o:(.bss+0x0): multiple definition of `mynamespace::lstr' cmakefiles/mylib.dir/src/il_class1.cpp.o:(.bss+0x0): first defined here collect2: error: ld returned 1 exit status make[2]: *** [libmylib.so] error 1 make[1]: *** [cmakefiles/mylib.dir/all] error 2 make: *** [all] error 2
so, when i'm preventing headers included twice, via #ifndef
, make
can't compile code because symbol declared in header detected twice. why?
after seeing question duplicated, got right solution.
my il_notify.h
#ifndef __il_notify_h__ #define __il_notify_h__ #include <string> #include "poco/logger.h" #include "poco/logstream.h" using poco::logger; using poco::logstream; namespace openil { // logger. inherits root channel extern logstream lstr; } #endif
my new il_notify.cpp
#include "il_notify.h" namespace openil { // todo: more flexibility here: more channels, different formatting... // maybe il_utils function should // logger. inherits root channel logstream lstr(logger::get("openillogger")); // other class, call logger way: //lstr << "this test" << std::endl; }
Comments
Post a Comment