我正在处理一个包含多个文件的项目,我需要登录每个文件。
为了编译文件,我需要以下内容:
/* 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' 被声明为外部,所以我不能将它重新定义为静态。