CMake linking shared library - cannot find lOgreMain

Patryk picture Patryk · Oct 10, 2012 · Viewed 7.7k times · Source

I have built myself Ogre3D http://ogre3d.org binaries and now I want to link to them my project in Qt ( I am using CMake as my build tool ). This is my CMakeLists.txt file :

project(ogre_face_test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})

include_directories( "/home/XXX/programming/SDKs/OGRE_1.8.1_BUILD/include" "." )

link_directories("/home/XXX/programming/SDKs/OGRE_1.8.1_BUILD/lib")
target_link_libraries( ogre_face_test "OgreMain" )

In .../BUILD/lib directory I have files like : libOgreMain.so, libOgreMain.so.1.8.1 and so on.

I am getting this error on linking in Qt Creator :

/usr/bin/cmake -H/home/XXX/programming/c++/ogre_face_sample/ogre_face_test -B/home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build/CMakeFiles /home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build'
make -f CMakeFiles/ogre_face_test.dir/build.make CMakeFiles/ogre_face_test.dir/depend
make[2]: Entering directory `/home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build'
cd /home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/XXX/programming/c++/ogre_face_sample/ogre_face_test /home/XXX/programming/c++/ogre_face_sample/ogre_face_test /home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build /home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build /home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build/CMakeFiles/ogre_face_test.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build'
make -f CMakeFiles/ogre_face_test.dir/build.make CMakeFiles/ogre_face_test.dir/build
make[2]: Entering directory `/home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build'
Linking CXX executable ogre_face_test
/usr/bin/cmake -E cmake_link_script CMakeFiles/ogre_face_test.dir/link.txt --verbose=1
/usr/bin/c++       CMakeFiles/ogre_face_test.dir/main.cpp.o CMakeFiles/ogre_face_test.dir/FacialAnimation.cpp.o  -o ogre_face_test -rdynamic -lOgreMain 
/usr/bin/ld: cannot find -lOgreMain
collect2: error: ld returned 1 exit status
make[2]: *** [ogre_face_test] Error 1
make[2]: Leaving directory `/home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build'
make[1]: *** [CMakeFiles/ogre_face_test.dir/all] Error 2
make[1]: Leaving directory `/home/XXX/programming/c++/ogre_face_sample/ogre_face_test-build'
make: *** [all] Error 2

EDIT

On the other hand if I use qmake instead of CMake with this file :

TARGET = qt_test_1
TEMPLATE = app

SOURCES += main.cpp\
        FacialAnimation.cpp

HEADERS  += FacialAnimation.h

INCLUDEPATH += /usr/include\
            /home/monkey/programming/SDKs/OGRE_1.8.1_BUILD/include
DEPENDPATH += /usr/include
LIBS += -L/home/monkey/programming/SDKs/OGRE_1.8.1_BUILD/lib -lOgreMain

it links fine.

Answer

Jan Hudec picture Jan Hudec · Oct 10, 2012

The CMake documentation says:

  • link_directories: Specify directories in which the linker will look for libraries.

    link_directories(directory1 directory2 ...)
    

    Specify the paths in which the linker should search for libraries. The command will apply only to targets created after it is called. For historical reasons, relative paths given to this command are passed to the linker unchanged (unlike many CMake commands which interpret them relative to the current source directory).

(emphasis mine) So you have to set the directories up before you call the add_executable.