7

私はこの簡単なコードを持っています:

#include <stdio.h>
#include <stdlib.h>

#include <GL/glew.h>
#include <GL/glfw.h>

int main(int argc, char const* argv[] )
{
    if( !glfwInit() ){
        fprintf( stderr, "failed\n" );
    }

    return 0;
}

そして私のCmakeLists.txtで:

PROJECT(test C)
find_package(OpenGL)
ADD_DEFINITIONS(
    -std=c99
    -lGL
    -lGLU
    -lGLEW
    -lglfw
)
SET(SRC test)
ADD_EXECUTABLE(test ${SRC})

「cmake」を実行しています。エラーは発生しませんが、make を実行すると次のように表示されます。

test.c:(.text+0x10): undefined reference to `glfwInit'
collect2: ld returned 1 exit status
make[2]: *** [tut1] Error 1

実行中:

gcc -o test test.c -std=c99 -lGL -lGLU -lGLEW -lglfw

エラーなしでコードを正常にコンパイルします。自分のコードで cmake を実行するにはどうすればよいですか?

また、これらの行をメイン関数に追加すると:

glfwOpenWindowHint( GLFW_FSAA_SAMPLES, 4 );
glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 3 );
glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 1 );
glfwOpenWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

同じフラグを指定して gcc を実行しても、エラーが発生します。

test.c: In function ‘main’:
test.c:14: error: ‘GLFW_OPENGL_VERSION_MAJOR’ undeclared (first use in this function)
test.c:14: error: (Each undeclared identifier is reported only once
test.c:14: error: for each function it appears in.)
test.c:15: error: ‘GLFW_OPENGL_VERSION_MINOR’ undeclared (first use in this function)
test.c:16: error: ‘GLFW_OPENGL_PROFILE’ undeclared (first use in this function)
test.c:16: error: ‘GLFW_OPENGL_CORE_PROFILE’ undeclared (first use in this function)

kubuntu 10.04、cmake v2.8、libglfw-dev、libglfw2、libglew1.5、libglew1.5-dev、glew-utils に基づいて Linux ミントを実行しています。

私はcmake、glew、glfwが初めてです。助けてくれてありがとう!

乾杯!

P

4

3 に答える 3

4

ここで、glfw で cmake を使用する方法の例を見ることができます。 http://code.google.com/p/assembly3d/source/browse/tools/viewer/CMakeLists.txt

glfw http://code.google.com/p/assembly3d/source/browse/tools/viewer/cmake_modules/FindGLFW.cmakeを見つけるためにFindGLFW.cmakeを使用します

また、ubuntu の glfw のバージョンは 2.6 です。GLFW_OPENGL_VERSION_MINOR と GLFW_OPENGL_VERSION_MAJOR は glfw 2.7 でのみ動作し、OpenGL 3.x は glfw 2.7 でのみ動作すると思います。

一番

于 2011-08-06T13:51:21.883 に答える
3

CMake によって生成された makefile が実行しているコマンドを表示するには、次のように make を実行します。

make VERBOSE=1

コマンドを表示すると、CMake プロジェクトをデバッグするときに非常に役立ちます。提供されている例では、次のコマンドが実行されます。

/usr/bin/gcc -std=c99 -lGL -lGLU -lGLEW -lglfw -o CMakeFiles/test.dir/test.c.o -c test.c
/usr/bin/gcc CMakeFiles/test.dir/test.o -o test -rdynamic

CMake によって生成されたメイクファイルは、各ソース ファイルをオブジェクト ファイルに個別にコンパイルし (これはgcc -cが行うことです)、個別のコマンドですべてのオブジェクト ファイルをリンクします。提供されている例では、リンク段階ではなく、コンパイル段階で OpenGL 関連のライブラリが指定されています。add_definitionsでライブラリを指定する代わりに、target_link_librariesコマンドを使用する必要があります。

次のような CMakeLists.txt ファイルが機能するはずです。

cmake_minimum_required(VERSION 2.8)
project(test C)
add_definitions(-std=c99)
set(SRC test.c)
add_executable(test ${SRC})
target_link_libraries(test GL GLU GLEW glfw)

target_link_librariesは、UNIX/Linux 環境の場合は -l プレフィックスを、Windows環境の場合は.lib拡張子を自動的に追加するため、ライブラリに -l プレフィックスを指定する必要はありませんtarget_link_libraries の詳細については、 http: //www.cmake.org/cmake/help/cmake-2-8-docs.html#command: target_link_librariesを参照してください。

于 2011-08-05T02:52:54.497 に答える
0

Pixar has did it yet. The below is its source code, you can refer it: https://github.com/PixarAnimationStudios/OpenSubdiv/blob/master/cmake/FindGLFW.cmake

于 2015-10-14T16:41:11.027 に答える