CMake not linking .obj files properly LNK2019 __thiscall

767 views Asked by At

Firstly, I've been working on this for days now and I've tried all possible fixes (went to 10th Google search page searching for a fix) but I can't get it to work.

I'm porting Unix app (G++/Bison/Flex) to Windows (MSVC/WinBison/WinFlex). I'm using CMake as a build system and the idea is to build the project from VS CMD and get VS project ready for modification. The project is meant to work on both platforms so all the modifications should be done in CMakeLists.txt so that I don't have to write special instructions done in VS.

Problematic line in preprocessor.cc is

State current(task->CPFs);

while the method State(std::vector<ConditionalProbabilityFunction*> const& cpfs) is declared in states.h with following code:

struct State { State(std::vector<ConditionalProbabilityFunction*> const& cpfs); State(State const& other) : state(other.state) {} State(int stateSize) : state(stateSize, 0.0) {}

and implemented in states.cc:

State::State(vector<ConditionalProbabilityFunction*> const& cpfs) { for (unsigned int i = 0; i < cpfs.size(); ++i) { state.push_back(cpfs[i]->getInitialValue()); } }

states.h is included in states.cc and rddl.h (which is included in preprocessor.cc) and class States is forward declared in preprocessor.h (which is included in preprocessor.cc).

The errors I'm getting are

[ 36%] Linking CXX executable rddl-parser.exe preprocessor.cc.obj : error LNK2019: unresolved external symbol "public: __thiscall State::State(class std::vector<struct ConditionalProbabilityFunction *,class std::allocator<struct ConditionalProbabilityFunction *> > const &)" (??0State@@QAE@ABV?$vector@PAUConditionalProbabilityFunction@@V?$allocator@PAUConditionalProbabilityFunction@@@std@@@std@@@Z) referenced in function "private: void __thiscall Preprocessor::prepareActions(void)" (?prepareActions@Preprocessor@@AAEXXZ) rddl.cc.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall LogicalExpression::evaluate(double &,struct State const &,class ActionState const &)const " (?evaluate@LogicalExpression@@UBEXAANABUState@@ABVActionState@@@Z)

Linking, when build on Unix, works perfectly. But when I run it on Windows, I get this error.

This is the part of CMake code that does the linking:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /DEBUG") set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG") add_executable(rddl-parser ${RDDL_PARSER_SOURCES} ${FLEX_scanner_OUTPUTS} ${BISON_parser_OUTPUTS}) target_link_libraries(rddl-parser ${FLEX_LIBRARIES} ${BISON_LIBRARIES})

The option to copy the definition of the function to the source file is a no go.

2

There are 2 answers

0
Đorđe Relić On BEST ANSWER

OK, there was some almost duplicate code in this project where I noted that State was defined as struct and not as a class. When I switched it to being defined as class with all public methods, the compilation passed. It's interesting that this wasn't an issue on Unix and is on Windows.

7
Luca Cappa On

Check whether the declaration of the function/class match exactly their definitions. It could happen that when you compile logical_expressions.cc you define something that is not identical to the forward declaration for preprocessor.cc and rddl.cc files.