[CMake] Help getting my simple OpenGL project to use cmake

Ryan Pavlik rpavlik at iastate.edu
Thu Jul 8 04:24:48 EDT 2010


  First of all - don't use file glob - see many past threads, FAQ 
entries, etc.  This is a common beginner mistake, with unfortunate 
consequences.

Here is my quickie re-write of your build. You might need to 
double-check the variable names with their respective find modules - I 
did them from memory but with the knowledge that the OpenGL and GLUT 
ones are a little funky when compared to the modern de-facto standard.

Main directory:

cmake_minimum_required (VERSION 2.6)
project(Pemberton)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
if(CMAKE_COMPILER_IS_GNUCXX)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -Werror")
endif()

include_directories(library ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS})
add_subdirectory(library)
add_subdirectory(programs)


Library directory:

set(LIB_SOURCES Affine.cpp
		Affine.h
		Camera.cpp
		Camera.h
		Canvas.cpp
		Canvas.h
		Color.cpp
		Color.h
		Light.cpp
		Light.h
		Material.cpp
		Material.h
		Matrix.cpp
		Matrix.h
		Quaternion.cpp
		Quaternion.h
		Ray.cpp
		Ray.h
		Texture.cpp
		Texture.h
		utility.cpp
		Vertex.cpp
		Vertex.h)
add_library(myGraphicsLib STATIC ${LIB_SOURCES})
target_link_libraries(myGraphicsLib ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})

Programs directory:

set(APPS exam
		forces
		ifs
		lsystem
		raytrace
		subdivide
		testray)
foreach(app ${APPS})
	add_executable(${app} ${APPS}.cpp)
	target_link_libraries(${app} myGraphicsLib)
endforeach()


Basic principles:
     Mention anything only once.
     Shared code: make a static lib
     Don't use outdated commands or style (subdirs, all caps commands, 
setting output directories without a compelling reason, using 
add_definitions to sneak compiler flags in)
     Minimize assumptions (-Wall -Werror only makes sense if you're 
using the gcc suite)

Didn't test it, but at the very least it should get you close.

Hope this helps!

Ryan

On 7/8/10 3:05 AM, Craig Pemberton wrote:
> Hello everyone,
>
> I am working on converting my project from using a handwritten
> Makefile to using cmake.
>
> I have been playing with cmake but I keep running into problems and
> I'm probably writing really ugly CMakeLists.txt files.
>
> My project is laid out like this:
>
> graphics/
> 	data/
> 		bark.rgb
> 		ground.rgb
> 		leaf.rgb
> 	library/
> 		Affine.cpp
> 		Affine.h
> 		Camera.cpp
> 		Camera.h
> 		Canvas.cpp
> 		Canvas.h
> 		Color.cpp
> 		Color.h
> 		Light.cpp
> 		Light.h
> 		Material.cpp
> 		Material.h
> 		Matrix.cpp
> 		Matrix.h
> 		Quaternion.cpp
> 		Quaternion.h
> 		Ray.cpp
> 		Ray.h
> 		Texture.cpp
> 		Texture.h
> 		utility.cpp
> 		Vertex.cpp
> 		Vertex.h
> 	programs/
> 		exam.cpp
> 		forces.cpp
> 		ifs.cpp
> 		lsystem.cpp
> 		raytrace.cpp
> 		subdivide.cpp
> 		testray.cpp
> My "library" is all the classes that I use while the programs are each
> files with a main function in them.
>
> My code relies on Glut and OpenGl.
>
> I would like cmake to build an executable for each of the files in
> programs and preferably build out of source so things stay relatively
> clean.
>
> Here is what my CMakeLists.txt files currently look like and I have no
> idea if I'm remotely doing the right thing.
>
> In the root directory /
>
> cmake_minimum_required (VERSION 2.6)
> project(Pemberton)
> find_package(GLUT)
> find_package(OpenGL)
> add_definitions(-Wall -Wextra -pedantic -Werror -O3)
> SUBDIRS(library programs)
> INCLUDE_DIRECTORIES(library)
> set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/library")
> set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}/bin")
> mark_as_advanced(LIBRARY_OUTPUT_PATH EXECUTABLE_OUTPUT_PATH)
>
> In library/
>
> FILE(GLOB SOURCE_FILES *.cpp)
> FILE(GLOB INCLUDE_FILES *.h)
> SOURCE_GROUP("Source Files" FILES ${SOURCE_FILES})
> SOURCE_GROUP("Header Files" FILES ${HEADER_FILES})
> ADD_LIBRARY (Graphics STATIC ${SOURCE_FILES} ${INCLUDE_FILES})
>
> In programs/
>
> include_directories(../library)
> #FILE(GLOB SOURCE_FILES .*.cpp)
> #FILE(GLOB INCLUDE_FILES *.h)
> #SOURCE_GROUP("Source Files" FILES ${SOURCE_FILES})
> #SOURCE_GROUP("Header Files" FILES ${HEADER_FILES})
> find_package(GLUT)
> find_package(OpenGL)
> add_executable(lsystem lsystem.cpp ${GLUT_LIBRARY} ${OPENGL_LIBRARY})
> #set(PROGRAMS ifs lsystem raytrace subdivide)
> #set(CORELIBS ${GLUT_LIBRARY} ${OPENGL_LIBRARY} GL GLU)
> #foreach(programs ${PROGRAMS})
> #	add_executable(${program} ${SOURCE_FILES} ${INCLUDE_FILES})
> 	#target_link_libraries(${program} ${CORELIBS})
> #endforeach(program)
>
> What is the correct way to set this up? Currently it's saying it can't
> find OpenGL functions. I would like to have some really nice clean
> cmake files.
>
> Best regards,
> Craig
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake


-- 
Ryan Pavlik
Human-Computer Interaction Graduate Student
Virtual Reality Applications Center
Iowa State University

rpavlik at iastate.edu
http://academic.cleardefinition.com/



More information about the CMake mailing list