[CMake] Figuring out dependencies for a library in order to build

E. Wing ewmailing at gmail.com
Mon Jul 18 18:52:06 EDT 2005


> OK, well if they hate autoconf, then the correct thing to do is to get the SDL
> to use CMake, and create Find/Use package files for SDL.  Then you just need
> to do a FIND_PACKAGE(SDL)  and it should work.
> 
> For the TRY_COMPILE stuff you use the RESULT_VAR and not the OUTPUT_VARIABLE.
> If it compiles then it should be OK.   It would seem that there is some missing
> documentation for TRY_COMPILE.   In addition to COMPILE_DEFINITIONS,
> INCLUDE_DIRECTORIES, LINK_DIRECTORIES, and LINK_LIBRARIES are also used by
> trycompile commands.
> 
> -Bill

I'm having trouble getting this to work. Can you clarify:
INCLUDE_DIRECTORIES, LINK_DIRECTORIES, and LINK_LIBRARIES, are they
additional parameters to TRY_COMPILE, or does TRY_COMPILE respect
these values set elsewhere? (Either way, I can't get it to work.)

I created a simple mock up. I have a file called test_sdlsound.c which contains:
#include "SDL_sound.h"
int main(int argc, char* argv[])
{
        Sound_Init();
        return 0;
}

I created a CMakeLists.txt which has the following:
PROJECT(MYTEST)
	
INCLUDE(${PROJECT_SOURCE_DIR}/FindSDL.cmake)

# Find SDL_sound.h
FIND_PATH(SDL_SOUND_INCLUDE_DIR SDL_sound.h
	~/Library/Frameworks/SDL_sound.framework/Headers
	/Library/Frameworks/SDL_sound.framework/Headers
	$ENV{SDLDIR}/include
	/usr/include/SDL
	/usr/include/SDL12
	/usr/include/SDL11
	/usr/include
	/usr/local/include/SDL
	/usr/local/include/SDL12
	/usr/local/include/SDL11
	/usr/local/include
	/sw/include
)
# Find the SDL_sound library
IF(${SDL_SOUND_INCLUDE_DIR} MATCHES ".framework")
	SET (SDL_SOUND_LIBRARY "-framework SDL_sound" CACHE STRING "SDL_sound
framework for OSX")
ELSE(${SDL_SOUND_INCLUDE_DIR} MATCHES ".framework")
	FIND_LIBRARY(SDL_SOUND_LIBRARY 
		NAMES SDL_sound
		PATHS
		$ENV{SDLDIR}/lib
		/usr/lib
		/usr/local/lib
		/sw/lib
	)
ENDIF(${SDL_SOUND_INCLUDE_DIR} MATCHES ".framework")

# Debug, make sure I found the stuff
MESSAGE("${SDL_SOUND_INCLUDE_DIR} ${SDL_INCLUDE_DIR}")

# Set the paths
INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR} ${SDL_SOUND_INCLUDE_DIR})
LINK_LIBRARIES(${SDL_SOUND_LIBRARY} ${SDL_LIBRARY})
	
TRY_COMPILE( 
	MY_RESULT
	${CMAKE_BINARY_DIR}
	${PROJECT_SOURCE_DIR}/test_sdlsound.c
	OUTPUT_VARIABLE MY_OUTPUT
)
IF(NOT ${MY_RESULT})
	MESSAGE("${MY_OUTPUT}")
ELSE(NOT ${MY_RESULT})
	MESSAGE("Passed")
ENDIF(NOT ${MY_RESULT})
	


My FindSDL.cmake module is a copy of the one I submitted to CMake.

When I try running the above code, the TRY_COMPILE seems to ignore my
settings for INCLUDE_DIRECTORIES and LINK_LIBRARIES. Reading the
MY_OUTPUT variable, I verify that SDL_sound.h is not found which
demonstrates that my INCLUDE_DIRECTORIES is being ignored.

I also tried adding an entry in TRY_COMPILE for the two settings, but
I got something about a recursive error so I removed it.

I also tried changing my compile line to:
TRY_COMPILE( 
	MY_RESULT
	${CMAKE_BINARY_DIR}
	${PROJECT_SOURCE_DIR}/test_sdlsound.c
 	COMPILE_DEFINITIONS "-I/Library/Frameworks/SDL.framework/Headers"
"-I/Library/Frameworks/SDL_sound.framework/Headers" "-framework
SDL.framework" "-framework SDL_sound"
	OUTPUT_VARIABLE MY_OUTPUT
)

At this point, the headers get found, but the linker complains about
undefined symbols for SDL_sound. It throws away my link flags because
they come to early for the compiler. So this verifies that my
LINK_LIBRARIES is being ignored.

Once I get past this problem, I'm expecting to see a long list of
errors about not finding symbols for ogg vorbis, smpeg, mikmod, etc,
etc, at which point I plan to regex the result string and start adding
the missing libraries.

Can you tell me how to fix this?

Thanks,
Eric


More information about the CMake mailing list