我尝试创建一个简单的项目,在该项目中,我使用了整个项目中常见的两个变量。
但是当我尝试编译时,出现以下错误
CMakeFiles\stage.dir/objects.a(utilities.cpp.obj):utilities.cpp:(.bss+0x0): multiple definition of `error'
CMakeFiles\stage.dir/objects.a(main.cpp.obj):main.cpp:(.bss+0x0): first defined here
CMakeFiles\stage.dir/objects.a(utilities.cpp.obj):utilities.cpp:(.bss+0x8): multiple definition of `con'
CMakeFiles\stage.dir/objects.a(main.cpp.obj):main.cpp:(.bss+0x8): first defined here
CMakeFiles\stage.dir/objects.a(utilities.cpp.obj):utilities.cpp:(.bss+0x10): multiple definition of `value'
CMakeFiles\stage.dir/objects.a(main.cpp.obj):main.cpp:(.bss+0x10): first defined here
collect2.exe: error: ld returned 1 exit status
我尝试在多个文件中使用error和con变量。 这是我所有文件的实际代码。
Main.cpp
#include "utilities.h"
#include <iostream>
#include <windows.h>
void connect() {
int errorStatus = serverConnect (49152,"localhost");
std::cout << "Error Message:" << errorStatus << std::endl;
Sleep(1000);
}
Utilities.h
#include "iec61850_client.h"
#include <iostream>
IedClientError error;
IedConnection con;
MmsValue* value;
Utilities.cpp
#include "utilities.h"
int serverConnect(int tcpPort, string hostname) {
con = IedConnection_create();
IedConnection_connect(con, &error, hostname.c_str(), tcpPort);
return error;
}
我试图用上面的代码做的事是我在我的Utility.h文件中定义了error和con变量,尽管正在使用它并包含在Utility.cpp中,而且我在main.cpp中也包含Utility.h >
我知道main.cpp和Utility.cpp中都包含了Utility.h,但如何克服这一问题。
Cmake
set(EXECUTABLES
main.cpp
models/utilities.cpp
)
add_executable(stage ${EXECUTABLES} )
include_directories(
${CMAKE_CURRENT_LIST_DIR}/headers
${CMAKE_CURRENT_LIST_DIR}/models
)