LINK_LIBRARIES: Targets may link only to libraries. CMake is dropping the item.

编译项目时,用到external library (matlab,需要用到它的libeng.so和libmx.so库),CMakeLists.txt中有这么一句:
LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64")
编译时会报如下警告和错误:

$ sudo make
-- Configuring done
WARNING: Target "myProject" requests linking to directory "/opt/MATLAB/R2012a/bin/glnxa64".  Targets may link only to libraries.  CMake is dropping the item.
-- Generating done
-- Build files have been written to: /opt/matlabEngine_example3_cmake
Linking CXX executable myProject
/usr/bin/ld: cannot find -leng
/usr/bin/ld: cannot find -lmx
collect2: ld returned 1 exit status
make[2]: *** [myProject] Error 1
make[1]: *** [CMakeFiles/myProject.dir/all] Error 2
make: *** [all] Error 2
错误提示很显然,找不到libeng.so和libmx.so库。搜索一下它们所在路径,就是在/opt/MATLAB/R2012a/bin/glnxa64目录下:
$ locate libeng.so
/opt/MATLAB/MATLAB_Compiler_Runtime/v717/bin/glnxa64/libeng.so
/opt/MATLAB/R2012a/bin/glnxa64/libeng.so

$ locate libmx.so
/opt/MATLAB/MATLAB_Compiler_Runtime/v717/bin/glnxa64/libmx.so
/opt/MATLAB/R2012a/bin/glnxa64/libmx.so

原来LINK_LIBRARIES是需要指明到具体的库文件名字(You need to specify the full path to the .so files)这样写就可以了:

LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64/libeng.so")
LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64/libmx.so")

改后再试,警告消失,成功编译完。
$ sudo make
-- Configuring done
-- Generating done
-- Build files have been written to: /opt/matlabEngine_example3_cmake
Linking CXX executable myProject
[100%] Built target myProject

LINK_LIBRARIES的使用和link_directories比较类似,这里也可以简单的替换上面2行为:
link_directories("/opt/MATLAB/R2012a/bin/glnxa64")
同样的作用。我在这里总结了link_directories, LINK_LIBRARIES和target_link_libraries的区别和使用小结。

参考:

http://www.ogre3d.org/addonforums/viewtopic.php?f=17&t=12618
https://groups.google.com/forum/#!msg/ceres-solver/Y2mV-vKXsZE/2FinShKM2PsJ

你可能感兴趣的:(matlab,cmake)