I'm working on a cross platform c++ application (windows & Linux). I'm using VS 2017 professional as my IDE and Cmake as my build tool. Code contains both c & c++ files. Currently I'm able to build the project in Windows but I'm having issues linking object files in Linux.
Tools:
- Visual Studio Professional 2017
- Cmake version 3.12.18081601-MSVC_2 (Windows)
- Cmake version 3.16.1 (Linux)
The project structure below is simplified.
├── Application│├── Windows││└──abc.exe│└── Linux│└──abc└── Source├── CMakeLists.txt ├── main.cpp├── foo.c├── include│├── foo1.h│└── others... (.cc/.h,)└── parser├── foo2.cpp├── foo2.h├── win64│└── lxml.lib├── linux│└── lxml.a└── (Other sources/headers)
./CMakeLists.txt
cmake_minimum_required (VERSION 3.8)project(fileparser C CXX)if (WIN32) add_definitions(/DWINDOWS) include_directories(Source Source/include Source/parser Source/parser/abc Source/parser/libxml) link_directories(shared/parser/win64/)else() include_directories(Source Source/include Source/parser Source/parser/abc Source/parser/libxml) link_directories(shared/parser/linux/)endif(WIN32)file(GLOB SOURCES Sources/*.cpp Sources/*.c Sources/parser/*.cpp)# Add source to this project's executable.if (WIN32) add_executable (fileparser ${SOURCES}) install(TARGETS fileparser DESTINATION Application/Windows/)else() add_executable (fileparser ${SOURCES}) install(TARGETS fileparser DESTINATION Application/Linux/)endif(WIN32)SET_TARGET_PROPERTIES(fileparser PROPERTIES DEBUG_POSTFIX "_debug")
Build in Windows is successful. I'm able to generate executable.Finally, when I do build in Linux, the following error is generated:
Copying files to remote machine...rsync -t --delete --delete-excluded -v -r --exclude=.vs --exclude=.git --exclude=.vs --exclude=.git /C/Users/source/repos/ParserProject/ rsync://user@localhost:61989/tempsending incremental file listsent 6276 bytes received 71 bytes 4231.33 bytes/sectotal size is 39943966 speedup is 6293.36Finished copying files.cd /var/tmp/build/h3h8hh31-c1ff-183f-af38-c33e400692fc/build/Linux-Debug;/usr/local/bin/cmake --build "/var/tmp/build/h3h8hh31-c1ff-183f-af38-c33e400692fc/build/Linux-Debug" -- ;[ 9%] Linking CXX executable fileparser_debugCMakeFiles/fileparser.dir/Source/ABC_Class.cpp.o: In function `ABCBase::loadFile(char const*)':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/ABC_Class.cpp:45: undefined reference to `getAbcPath'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/ABC_Class.cpp:51: undefined reference to `getTmpPath'CMakeFiles/fileparser.dir/Source/Control.cpp.o: In function `main':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/Control.cpp:125: undefined reference to `dlclose'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/Control.cpp:126: undefined reference to `dlclose'CMakeFiles/fileparser.dir/Source/parser/XmlParser.cpp.o: In function `XmlParser::parse()':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:93: undefined reference to `xmlReaderForFile'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:99: undefined reference to `xmlTextReaderConstLocalName'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:102: undefined reference to `xmlTextReaderConstLocalName'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:119: undefined reference to `xmlFreeTextReader'CMakeFiles/fileparser.dir/Source/parser/XmlParser.cpp.o: In function `XmlParser::parseElementAttributes(Element*, bool)':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:129: undefined reference to `xmlTextReaderName'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:130: undefined reference to `xmlTextReaderValue'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:142: undefined reference to `xmlFree'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:143: undefined reference to `xmlFree'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:128: undefined reference to `xmlTextReaderMoveToNextAttribute'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:137: undefined reference to `xmlFree'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:138: undefined reference to `xmlFree'CMakeFiles/fileparser.dir/Source/parser/XmlParser.cpp.o: In function `XmlParser::parseChildElements(Element*)':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:152: undefined reference to `xmlTextReaderIsEmptyElement'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:161: undefined reference to `xmlTextReaderNodeType'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:162: undefined reference to `xmlTextReaderConstLocalName'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:163: undefined reference to `xmlTextReaderDepth'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:164: undefined reference to `xmlTextReaderIsEmptyElement'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:167: undefined reference to `xmlTextReaderDepth'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:160: undefined reference to `xmlTextReaderNodeType'CMakeFiles/fileparser.dir/Source/parser/XmlParser.cpp.o: In function `XmlParser::parseEndElement()':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:182: undefined reference to `xmlTextReaderNodeType'CMakeFiles/fileparser.dir/Source/parser/XmlParser.cpp.o: In function `XmlParser::parseSkipChildElement()':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:191: undefined reference to `xmlTextReaderDepth'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:198: undefined reference to `xmlTextReaderDepth'CMakeFiles/fileparser.dir/Source/parser/XmlParser.cpp.o: In function `XmlParser::readNextInXml()':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:205: undefined reference to `xmlTextReaderRead'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/parser/XmlParser.cpp:206: undefined reference to `xmlTextReaderNodeType'CMakeFiles/fileparser.dir/Source/xmlVersionParser.c.o: In function `readNextInXml':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:34: undefined reference to `xmlTextReaderRead'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:35: undefined reference to `xmlTextReaderNodeType'CMakeFiles/fileparser.dir/Source/xmlVersionParser.c.o: In function `extractFmiVersionAttribute':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:45: undefined reference to `xmlTextReaderGetAttribute'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:46: undefined reference to `_strdup'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:47: undefined reference to `xmlFree'CMakeFiles/fileparser.dir/Source/xmlVersionParser.c.o: In function `streamFile':/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:55: undefined reference to `xmlReaderForFile'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:59: undefined reference to `xmlTextReaderConstLocalName'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:60: undefined reference to `xmlTextReaderConstLocalName'/var/tmp/src/h3h8hh31-c1ff-183f-af38-c33e400692fc/Linux-Debug/ParserProject/Source/xmlVersionParser.c:68: undefined reference to `xmlFreeTextReader'collect2: error: ld returned 1 exit statusgmake[2]: *** [ParserProject/fileparser_debug] Error 1gmake[1]: *** [ParserProject/CMakeFiles/fileparser.dir/all] Error 2gmake: *** [all] Error 2Build failed.
I'm resolve this issue using target_link_options. But linking is still causing the issues.