1

我正在处理一个包含多个文件的项目,我需要登录每个文件。

为了编译文件,我需要以下内容:

/* Define the stock front-end process identity, so that it links when using
 * fe.N, fe.simple, etc. */
PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PSTR("fileX.cpp");

假设,我有两个文件共有的 log1.cpp 和 log2.cpp 和 log.h。这些文件被编译为 log1.o 和 log2.o。这工作得很好。

现在,当我将这两个文件链接到一个可执行文件中时,我收到以下错误:

log2.o:(.rodata+0x11): multiple definition of `PANTHEIOS_FE_PROCESS_IDENTITY'
log1.o:(.rodata+0x45): first defined here

现在的问题是,需要在 file1.cpp 和 file2.cpp 中定义 PANTHEIOS_FE_PROCESS_IDENTITY 才能编译。

我需要如何更改我的代码才能将其链接到可执行文件中?

以下是使用的文件:log1.cpp:

#include "log.h"

const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PSTR("log1.cpp");

int main()
{
  PANTHEIOS_TRACE_NOTICE(PSTR("a string at NOTICE level"));
  return 0;
}

日志2.cpp:

#include "log.h"
const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PSTR("log1.cpp");

日志.h:

#ifndef LOG_H
#define LOG_H

/* Pantheios Header Files */
#include <pantheios/pantheios.h>                // Pantheios C main header
#ifndef STLSOFT_CF_SUPPORTS_VARIADIC_MACROS
# error This example uses the Tracing API, which requires that the compiler support variadic macros
#endif /* !STLSOFT_CF_SUPPORTS_VARIADIC_MACROS */
#ifdef STLSOFT_CF_FUNCTION_SYMBOL_SUPPORT
# include <string>
# define PANTHEIOS_TRACE_PREFIX     \
    (   std::basic_string< PANTHEIOS_NS_QUAL(pan_char_t)>(__FILE__ " " PANTHEIOS_STRINGIZE(__LINE__) ": ") + \
        __FUNCTION__ + \
        "(): " \
    ).c_str()
#endif /* STLSOFT_CF_FUNCTION_SYMBOL_SUPPORT */
#include <pantheios/trace.h>                    // Pantheios Trace API
#include <pantheios/pantheios.hpp>              // Pantheios C++ main header

/* Standard C/C++ Header Files */
#include <exception>                            // for std::exception
#include <new>                                  // for std::bad_alloc
#include <string>                               // for std::string
#include <stdlib.h>                             // for exit codes

#ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
# if defined(STLSOFT_COMPILER_IS_MSVC)
#  pragma warning(disable : 4702)
# endif /* compiler */
#endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */

#define PSTR(x)         PANTHEIOS_LITERAL_STRING(x)

#include "pantheios/frontends/fe.simple.h" //for pantheios_fe_simple_setSeverityCeiling(level);

#endif //LOG_H

和make的输出:

g++ log1.cpp -c -I../pantheios-1.0.1-beta213/include -I../stlsoft-1.9.112/include
g++ log2.cpp -c -I../pantheios-1.0.1-beta213/include -I../stlsoft-1.9.112/include
g++ -o log log1.o log2.o -L../pantheios-1.0.1-beta213/lib \
        -lpantheios.1.core.gcc44\
        -lpantheios.1.be.fprintf.gcc44 -lpantheios.1.bec.fprintf.gcc44\
        -lpantheios.1.fe.simple.gcc44 -lpantheios.1.util.gcc44
log2.o:(.rodata+0x0): multiple definition of `PANTHEIOS_FE_PROCESS_IDENTITY'
log1.o:(.rodata+0x1): first defined here
/usr/bin/ld: Warning: size of symbol `PANTHEIOS_FE_PROCESS_IDENTITY' changed from 14 in log1.o to 9 in log2.o
collect2: ld returned 1 exit status

编辑:在 pantheios-1.0.1-beta213/include/pantheios/frontends/stock.h:120 'PANTHEIOS_FE_PROCESS_IDENTITY' 被声明为外部,所以我不能将它重新定义为静态。

4

3 回答 3

2

如果我的问题/答案听起来很奇怪,我深表歉意:但为什么要在每个 .cpp 文件中定义 PANTHEIOS_FE_PROCESS_IDENTITY 呢?PANTHEIOS_FE_PROCESS_IDENTITY 只需要在每个日志进程中定义一次,并在每行的开头打印到日志语句中。例如,来自我自己的基于 pantheios 的记录器项目:

[l.SPP.6408,2012 年 4 月 7 日下午 6:28:44.702;注意]: .\Log.cpp(168): CLogApp::InitInstance: STARTING LOGGING

我只定义了 1 个 PANTHEIOS_FE_PROCESS_IDENTITY - 在 Log.cpp(不是 .h)中。如下,不仅适用于 LOG 项目,还适用于我的解决方案(VS2005)中需要访问记录器的其他项目:

PANTHEIOS_EXTERN_C const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "l.SPP";

You will get the filename+line-number from where the log statement is being printed out if you use PANTHEIOS_TRACE_NOTICE - see the example I posted above (in bold).

I suggest that you determine if PANTHEIOS_FE_PROCESS_IDENTITY really needs to be defined per-cpp file or per-project - my experience shows the latter to be the answer.

于 2012-04-09T17:20:29.817 回答
0

这取决于PANTHEIOS_EXTERN_C扩展到什么。如果它扩展到extern- 很可能 - 你会得到错误。

我要么完全删除它,要么取消定义它。(我会选择第一个,不需要它extern

请注意,在这种情况下,PANTHEIOS_FE_PROCESS_IDENTITY不是全局的。如果你想让它成为全球性的,你将它声明为 extern只在一个翻译单元中定义它。

所以:

如果PANTHEIOS_FE_PROCESS_IDENTITY应该是全局的:

//log1.cpp

const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PSTR("fileX.cpp"); 
//definition here

//log2.cpp

PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[]; 
//declared as extern here

如果没有,并且您希望 2 个文件有所不同:

//log1.cpp

const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PSTR("fileX.cpp"); //not extern
                                                                      //definition

//log2.cpp

const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PSTR("fileX.cpp"); //not extern
                                                                      //definition
于 2012-04-03T14:30:39.793 回答
0

如果您想要const PANTHEIOS_FE_PROCESS_IDENTITY一个文件 file1.cpp 和另一个const PANTHEIOS_FE_PROCESS_IDENTITY文件 file2.cpp 中的另一个,则将其声明为static并且不会导出到文件之外。

PANTHEIOS_EXTERN_C static const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PSTR("fileX.cpp");
于 2012-04-03T15:05:24.700 回答