<div dir="ltr"><div><div><div><div>It turns out this is not a CMake issue.<br><br></div>Sorry for the wrong question. I switched from a makefile to CMake at the same time I switched from one cpp to 3 cpp in my little project.<br><br></div>The problem occurs when "CL/cl2.hpp" is used in multiple .cpp files, with or without using CMake to build.<br><br></div>I still don't know why the linker sees multiple definitions, it turns out it's from a function like this in the header:<br><br>namespace detail<br>{<br>    static inline cl_int errHandler (cl_int err, const char * errStr = NULL)<br>    {<br>        if (err != CL_SUCCESS)<br>            throw Error(err, errStr);<br><br>        return err;<br>    }<br>}<br><br></div>(which I understand should be safe to use in a .hpp)<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 23, 2017 at 2:05 PM, Timothy Madden <span dir="ltr"><<a href="mailto:terminatorul@gmail.com" target="_blank">terminatorul@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div><div>Hello,<br><br></div>In my CMakeLists.txt I have a simple executable target like:<br><br>cmake_minimum_required(VERSION 3.2)  # I actually have 3.10 installed<br>project(cl-tool CXX)<br><br>set(OPENCL_INCLUDE_SEARCH_PATH<wbr>S ...)<br>set(OPENCL_LIBRARY_64_SEARCH_P<wbr>ATHS ...)<br><br>if (CYGWIN)<br>    set(CMAKE_FIND_LIBRARY_SUFFIXE<wbr>S .lib)<br>    set(OCL_LIB_SUFFIX .lib)<br>endif()<br><br>set(OPENCL_LIBRARY_SEARCH_PATH<wbr>S ${OPENCL_LIBRARY_64_SEARCH_PAT<wbr>HS})<br>set(FIND_LIBRARY_USE_LIB64_PAT<wbr>HS ON)</div><div><br></div><div>find_path(OPENCL_INCLUDE_DIRS CL/cl.h PATHS ENV OpencCL_INCPATH ${OPENCL_INCLUDE_SEARCH_PATHS}<wbr>)<br>find_path(OPENCL2_HPP_INCLUDE_<wbr>DIRS CL/cl2.hpp PATHS ENV OpencCL_INCPATH ${OPENCL_INCLUDE_SEARCH_PATHS}<wbr>)<br><br>find_library(OPENCL_LIBRARIES NAMES OpenCL DOC "OpenCL library" PATHS ENV OpenCL_LIBPATH ${OPENCL_LIBRARY_SEARCH_PATHS}<wbr>)<br>find_package(Threads)<br><br>set(CL_INFO_SOURCES cl-matrix-mult.hh cl-matrix-mult.cc cl-platform-info.hh cl-platform-info.cc cl-tool.cc)<br><b>add_executable(cl-tool ${CL_INFO_SOURCES})</b><br>target_compile_features(cl-too<wbr>l PRIVATE cxx_std_14)<br>target_compile_definitions(cl-<wbr>tool PRIVATE CL_HPP_TARGET_OPENCL_VERSION=1<wbr>20 CL_HPP_MINIMUM_OPENCL_VERSION=<wbr>110 CL_HPP_CL_1_2_DEFAULT_BUILD CL_HPP_ENABLE_EXCEPTIONS)<br>target_compile_definitions(cl-<wbr>tool PRIVATE __CL_ENABLE_EXCEPTIONS CL_VERSION_1_2)<br>target_include_directories(cl-<wbr>tool PRIVATE ${OPENCL_INCLUDE_DIRS} ${OPENCL2_HPP_INCLUDE_DIRS})<br>target_compile_options(cl-tool PRIVATE -Wno-ignored-attributes -fvisibility=hidden)<br>target_link_libraries(cl-tool ${OPENCL_LIBRARIES} Threads::Threads)<br><br></div>But then at build time with:</div><div><br></div><div>   cd build && cmake .. -G "MinGW ..." -DCMAKE_CXX_COMPILER=g++</div><div>   gmake VERBOSE=1</div><div><br></div><div>I get bellow unexpected behaviour:<br><br>CMake is building a static archive with `ar` containing all 3 objects in my target executable cl-tool.exe.<br></div>The static archive is then linked in the executable with -Wl,--whole-archive. CMake also wants to output an import library,<br></div>Although the target is clearly an executable named cl-tool.exe, on the same g++ command line used for linking.<br><br></div>My
 objects use some templates that are instantiated in 2 of the 3 objects,
 and using --Wl,--whole-archive with these object files,<br></div>results in multiple definitions for the instantiations. <br></div><div><br></div><div>Normally the linker would see the multiple symbols as weak references and discard duplicates, but now CMake insists on using</div><div>--whole-archive on the linker command line, so duplicates now result in an error.</div><div><br></div><div>I have Win10 up-to-date, CMake 3.10, and g++ 7.1, from the MinGW installation that comes packaged with Strawberry Perl.</div><div>Also Intel OpenCL SDKincluded and linked with.<br></div><div><br></div><div>This
 unexpected behaviour is breaking my build, and I am also worried about 
the duplicate / unused code in the resulting executable.</div><div>Is there a proper solution to this problem please ?</div><div><br></div><div>Why does CMake go to the trouble of:</div><div>  - creating a separate static archive with the 3 object files (which is not requested from the CMakeLists.txt file) ?<br></div><div>  - using --whole-archive on the resulting .a file instead of linking normally ?</div><div>  - attempt to output the import library for an .exe module ?</div><div><br></div><div>Apparently CMake believes my target is a .dll (shared library), but nowhere in the CMakeLists is a .dll ever created.</div><div><br></div>I set ENABLE_EXPORTS and CMAKE_WIN32_EXPORT_ENABLE to "OFF", with no effect.<br></div><div><br></div><div>--<br></div>Thank you,<br></div>Timothy Madden<br></div>
</blockquote></div><br></div>