[CMake] Adding a library to link in during TRY_RUN with Windows/nmake?

Clifford Yapp cliffyapp at gmail.com
Tue Oct 12 10:00:36 EDT 2010


I'm trying to compile some C code using TRY_RUN that uses gethostname,
but in order for this code to succeed on Windows it needs "/link
ws2_32.lib" added to the compile line.  I have tried putting this
string into the ARGS variable of TRY_RUN and a couple of CMake
variables, but the compile still fails and I can't see any indications
the extra link argument is being passed... am I doing something wrong?
 Alternately, does anybody know of a pre-existing cross-platform way
to get the hostname from CMake?

#include <stdio.h>
#include <WinSock2.h>
#define chararraysize 2048
int main(void) {
  int i;
  char hostname[chararraysize];
  FILE *outfp = NULL;

  hostname[chararraysize - 1] = "\0";
  gethostname(hostname, chararraysize - 1);
  outfp = fopen("${CMAKE_BINARY_DIR}/include/conf/HOST", "w");
  fprintf(outfp, "\"%s\"", hostname);
  fclose(outfp);
  return 0;
}

TRY_RUN(HOSTNAME_RESULT HOSTNAME_COMPILED
	${CMAKE_BINARY_DIR}/CMakeTmp
	${CMAKE_BINARY_DIR}/CMakeTmp/report_hostname.c
	OUTPUT_VARIABLE COMPILEMESSAGES
	ARGS "/link ws2_32.lib")

Change Dir: C:/Users/User/Documents/brlcad/cmake-build/CMakeTmp/CMakeFiles/CMakeTmp

Run Build Command:nmake /NOLOGO  "cmTryCompileExec\fast"
	"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\nmake.exe"
-f CMakeFiles\cmTryCompileExec.dir\build.make /nologo -L
   CMakeFiles\cmTryCompileExec.dir\build
	"C:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -E
cmake_progress_report
C:\Users\User\Documents\brlcad\cmake-build\CMakeTmp\CMakeFiles\CMakeTmp\CMakeFiles
1
Building C object CMakeFiles/cmTryCompileExec.dir/report_hostname.c.obj
	C:\PROGRA~2\MICROS~1.0\VC\bin\cl.exe
@C:\Users\User\AppData\Local\Temp\nm9905.tmp
report_hostname.c
C:\Users\User\Documents\brlcad\cmake-build\CMakeTmp\report_hostname.c(9)
: warning C4047: '=' : 'char' differs in levels of indirection from
'char [2]'
C:\Users\User\Documents\brlcad\cmake-build\CMakeTmp\report_hostname.c(11)
: warning C4996: 'fopen': This function or variable may be unsafe.
Consider using fopen_s instead. To disable deprecation, use
_CRT_SECURE_NO_WARNINGS. See online help for details.
        C:\Program Files (x86)\Microsoft Visual Studio
9.0\VC\INCLUDE\stdio.h(237) : see declaration of 'fopen'
C:\Users\User\Documents\brlcad\cmake-build\CMakeTmp\report_hostname.c(5)
: warning C4101: 'i' : unreferenced local variable
Linking C executable cmTryCompileExec.exe
	"C:\Program Files (x86)\CMake 2.8\bin\cmake.exe" -E vs_link_exe
C:\PROGRA~2\MICROS~1.0\VC\bin\cl.exe  /nologo
@CMakeFiles\cmTryCompileExec.dir\objects1.rsp
@C:\Users\User\AppData\Local\Temp\nm9A8C.tmp
report_hostname.c.obj : error LNK2019: unresolved external symbol
__imp__gethostname at 8 referenced in function _main

cmTryCompileExec.exe : fatal error LNK1120: 1 unresolved externals

LINK Pass 1 failed. with 2
NMAKE : fatal error U1077: '"C:\Program Files (x86)\CMake
2.8\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual
Studio 9.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.


More information about the CMake mailing list