I have a CMake build that sends /IMPLIB to the linker on Windows. This is a problem in my case because the argument to implib is the same path as one of the input files. It looks to me that CMake will always issue /IMPLIB when building with Visual Studio, and the passed argument cannot be modified. Is there a way to control this behaviour?
-
What for names you give input files to be the same as an import library? And what you mean with "the passed argument cannot be modified"? Cannot or you just don't know how?– YoukaJan 3, 2016 at 12:05
-
@Youka if I understand cyou're asking why the input file and the import library have the same name. This is accidental in that, the product can be deployed either as a library or as an executable derived from the same library, so the lib is foo.lib, the exe is foo.exe. CMake apparently tries to build an import library, which it names... foo.lib. In practice this library would not be created (there are no symbols to export!) but MSBuild will detect the name conflict and stop building.– user234736Jan 4, 2016 at 3:53
-
1@Youka I believe that the passed argument can only be modified by changing the product name, which surprises me a little. In other words I expect that if I were building a library called foo.dll, it should be possible (though ill advised!) to name the import library bar.dll. But I haven't found a way to do so. Stopping CMake from issuing /IMPLIB would be ideal; finally I don't see the use of /IMPLIB in conjunction with building an executable.– user234736Jan 4, 2016 at 3:56
3 Answers
I don't think its possible to prevent CMake from issuing an /IMPLIB
option to the linker.
You can however control the name of the generated import library by setting the following properties of a shared library target:
add_library(foo SHARED foo.cpp)
# set base name of generated DLL import library
set_target_properties(foo PROPERTIES ARCHIVE_OUTPUT_NAME "bar")
# set prefix of generated DLL import library
set_target_properties(foo PROPERTIES IMPORT_PREFIX "")
# set suffix of generated DLL import library
set_target_properties(foo PROPERTIES IMPORT_SUFFIX ".lib")
The name of the generated shared library can be adjusted by setting the following target properties:
# set base name of generated DLL shared library
set_target_properties(foo PROPERTIES RUNTIME_OUTPUT_NAME "bar")
# set prefix of generated DLL shared library
set_target_properties(foo PROPERTIES PREFIX "")
# set suffix of generated DLL shared library
set_target_properties(foo PROPERTIES SUFFIX ".dll")
Looking at CMake's source cmComputeLinkInformation.cxx
it will only add a valid /implib:...
option if CMAKE_IMPORT_LIBRARY_SUFFIX
is set:
// Check whether we should use an import library for linking a target. this->UseImportLibrary = this->Makefile->IsDefinitionSet("CMAKE_IMPORT_LIBRARY_SUFFIX");
So in the following test the import library was removed from my executable project's options:
cmake_minimum_required(VERSION 3.0)
project(NoImpLib CXX)
unset(CMAKE_IMPORT_LIBRARY_SUFFIX)
file(WRITE main.cpp "int main() { return 0; }")
add_executable(${PROJECT_NAME} main.cpp)
An VS specific alternative - because this option is not set otherwise/per configuration - would be to add the global property IgnoreImportLibrary
with:
set_target_properties(${PROJECT_NAME} PROPERTIES VS_GLOBAL_IgnoreImportLibrary "true")
-
Unsetting
CMAKE_IMPORT_LIBRARY_SUFFIX
just results in the import library argument being set to an incomplete path ending a directory (under 3.12-rc3).– detlyJul 9, 2018 at 22:36 -
Also your second suggestion (a) should be set on the target, not project, and (b) does not result in the
implib
flag appearing when linking that target.– detlyJul 9, 2018 at 23:05
This answer suggests adding the /noimplib
option to target_link_options()
should prevent the generation of an import library, but in my experience, it still gets generated.
This e-mail has the answer that worked for me; simply avoid specifying ARCHIVE DESTINATION
and LIBRARY DESTINATION
in your install()
command.