1

POCO C ++ライブラリとリンクするコードをコンパイルして、Linux用の共有ライブラリを作成するのに問題があります(念のため、Mint 13)。これが私の縮小環境です。

私はこれらの3つのファイルを持っています:

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
{
    // Get logger. Inherits root channel
    LogStream lstr(Logger::get("MyLogger"));

    // From any other class, call logger this way:
    //lstr << "This is a test" << std::endl;
}

#endif

IL_Class1.cpp

#include "IL_Class1.h"
#include "IL_Notify.h"

namespace mynamespace {

    void IL_Class1::foo()
    {
        // Stuff...

        lstr << "This is a test msg from 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 is a test msg from IL_Class2::bar" << std::endl;
    }
}

IL_Class1IL_Class2は と で宣言されIL_Class1.hIL_Class2.hいます。IL_Notify.hこれらのヘッダー内には含まれません。

そして、これは私のCMakeLists.txtファイルです

# Set search path to find PocoConfig.cmake
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_SOURCE_DIR}/cmake/Modules/)


# Look for needed packages
find_package(Poco REQUIRED)

# Now, we can use poco
include_directories(
    ${Poco_INCLUDE_DIRS}
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# Libraries which we are linking against
link_directories(
    ${Poco_LIBRARY_DIRS}
)

# Get all the cpp files to build my 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})

正常に実行cmakeでき、Makefile. しかし、実行するmakeと、このエラーが発生します

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

そのため、ヘッダーが 2 回含まれないようにしていても、そのヘッダーで宣言されたシンボルが 2 回検出されるため#ifndefmakeコードをコンパイルできません。なんで?

4

1 に答える 1

1

私の質問が重複しているのを見た後、私は正しい解決策を得ました。

じぶんの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
{
    // Get logger. Inherits root channel
    extern LogStream lstr;
}

#endif

私の新しい IL_Notify.cpp

#include "IL_Notify.h"

namespace openil
{
    // TODO: More flexibility here: more channels, different formatting...
    // Maybe a IL_Utils function should do that

    // Get logger. Inherits root channel
    LogStream lstr(Logger::get("OpenILLogger"));

    // From any other class, call logger this way:
    //lstr << "This is a test" << std::endl;
}
于 2014-04-15T10:28:37.753 に答える