I have been trying to link OpenImageIO to an executable using CMake, but keep getting a error during the linking stage of the build. Does anyone know how to set up the CMakeLists.txt correctly so the library will link?
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
project(VolumeFX)
add_compile_options(-std=c++11)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
include_directories("/usr/local/include")
find_library(OIIO "OpenImageIO" "/usr/local/lib")
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
include_directories("/group/dpa/include")
find_library(OIIO "OpenImageIO")
endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(SOURCE_FILES
main.cpp
Vector.h
Camera.cpp
Camera.h
FloatVolume.h
SphereVolume.cpp
SphereVolume.h
ClampMin.cpp
ClampMin.h
Renderer.cpp
Renderer.h
Image.cpp
Image.h
OIIOFiles.cpp
OIIOFiles.h)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_link_libraries(${PROJECT_NAME} ${OIIO})
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(${PROJECT_NAME} ${OIIO})
endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
Here is the output from running the make file...
Scanning dependencies of target VolumeFX
[ 14%] Building CXX object CMakeFiles/VolumeFX.dir/main.cpp.o
[ 28%] Building CXX object CMakeFiles/VolumeFX.dir/Camera.cpp.o
[ 42%] Building CXX object CMakeFiles/VolumeFX.dir/SphereVolume.cpp.o
[ 57%] Building CXX object CMakeFiles/VolumeFX.dir/ClampMin.cpp.o
[ 71%] Building CXX object CMakeFiles/VolumeFX.dir/Renderer.cpp.o
[ 85%] Building CXX object CMakeFiles/VolumeFX.dir/Image.cpp.o
[100%] Building CXX object CMakeFiles/VolumeFX.dir/OIIOFiles.cpp.o
Linking CXX executable VolumeFX
CMakeFiles/VolumeFX.dir/OIIOFiles.cpp.o: In function `lux::writeOIIOImage(char const*, lux::Image&, float, float)':
OIIOFiles.cpp:(.text+0x345): undefined reference to `OpenImageIO::v1_5::ImageOutput::create(std::string const&, std::string const&)'
OIIOFiles.cpp:(.text+0x435): undefined reference to `OpenImageIO::v1_5::ImageSpec::ImageSpec(int, int, int, OpenImageIO::v1_5::TypeDesc)'
CMakeFiles/VolumeFX.dir/OIIOFiles.cpp.o: In function `lux::writeOIIOImage(char const*, lux::Image&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&, float, float)':
OIIOFiles.cpp:(.text+0x18a7): undefined reference to `OpenImageIO::v1_5::ImageOutput::create(std::string const&, std::string const&)'
OIIOFiles.cpp:(.text+0x1997): undefined reference to `OpenImageIO::v1_5::ImageSpec::ImageSpec(int, int, int, OpenImageIO::v1_5::TypeDesc)'
CMakeFiles/VolumeFX.dir/OIIOFiles.cpp.o: In function `lux::readOIIOImage(char const*, lux::Image&)':
OIIOFiles.cpp:(.text+0x2b8b): undefined reference to `OpenImageIO::v1_5::ImageInput::create(std::string const&, std::string const&)'
OIIOFiles.cpp:(.text+0x2c14): undefined reference to `OpenImageIO::v1_5::ImageSpec::ImageSpec(OpenImageIO::v1_5::TypeDesc)'
CMakeFiles/VolumeFX.dir/OIIOFiles.cpp.o: In function `lux::readOIIOImage(char const*, lux::Image&, std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > >&)':
OIIOFiles.cpp:(.text+0x3011): undefined reference to `OpenImageIO::v1_5::ImageInput::create(std::string const&, std::string const&)'
OIIOFiles.cpp:(.text+0x309a): undefined reference to `OpenImageIO::v1_5::ImageSpec::ImageSpec(OpenImageIO::v1_5::TypeDesc)'
OIIOFiles.cpp:(.text+0x339e): undefined reference to `OpenImageIO::v1_5::ImageSpec::metadata_val(OpenImageIO::v1_5::ParamValue const&, bool) const'
CMakeFiles/VolumeFX.dir/OIIOFiles.cpp.o: In function `OpenImageIO::v1_5::ParamValue::~ParamValue()':
OIIOFiles.cpp:(.text._ZN11OpenImageIO4v1_510ParamValueD2Ev[_ZN11OpenImageIO4v1_510ParamValueD5Ev]+0x14): undefined reference to `OpenImageIO::v1_5::ParamValue::clear_value()'
CMakeFiles/VolumeFX.dir/OIIOFiles.cpp.o: In function `OpenImageIO::v1_5::ImageSpec::attribute(OpenImageIO::v1_5::string_view, OpenImageIO::v1_5::string_view)':
OIIOFiles.cpp:(.text._ZN11OpenImageIO4v1_59ImageSpec9attributeENS0_11string_viewES2_[_ZN11OpenImageIO4v1_59ImageSpec9attributeENS0_11string_viewES2_]+0x1c): undefined reference to `OpenImageIO::v1_5::string_view::c_str() const'
OIIOFiles.cpp:(.text._ZN11OpenImageIO4v1_59ImageSpec9attributeENS0_11string_viewES2_[_ZN11OpenImageIO4v1_59ImageSpec9attributeENS0_11string_viewES2_]+0x66): undefined reference to `OpenImageIO::v1_5::ImageSpec::attribute(OpenImageIO::v1_5::string_view, OpenImageIO::v1_5::TypeDesc, void const*)'
collect2: error: ld returned 1 exit status
make[2]: *** [VolumeFX] Error 1
make[1]: *** [CMakeFiles/VolumeFX.dir/all] Error 2
make: *** [all] Error 2
You used wrong variable name to refer OpenImageIO libraries.
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
target_link_libraries(${PROJECT_NAME} ${OPENIMAGEIO_LIBRARIES})
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_link_libraries(${PROJECT_NAME} ${OPENIMAGEIO_LIBRARIES})
endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
Related
I am trying to include some libraries i downloaded using vcpkg. I want to use all of them from vcpkg instead of system libraries.
I am able to run cmake .. command in build directory. It is successful.
Then i run make command but i am getting lots of "undefined reference to" error for all libraries i linked.
Here is CmakeLists.txt:
set(CMAKE_TOOLCHAIN_FILE "/home/ubuntu-user/Desktop/flight-simulator/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(PROJECT_NAME Simulator)
cmake_minimum_required(VERSION 3.12)
project(${PROJECT_NAME})
##OPENGL
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIRS})
##GLAD
find_path(GLAD_INCLUDE_DIR glad/glad.h)
find_library(GLAD_LIBRARY glad)
#message(${GLAD_INCLUDE_DIR})
#ASSIMP
find_library(ASSIMP_LIBRARY assimp)
#IMGUI
set(IMGUI_DIR ./libs/imgui-implot)
add_library(imgui STATIC
${IMGUI_DIR}/imgui_impl_glfw.cpp
${IMGUI_DIR}/imgui.cpp
)
#SOURCES
FILE(GLOB SOURCES
"src/Data.cpp"
"src/Frames.cpp"
"src/Model.cpp"
"src/main.cpp"
)
#EXECUTABLE
ADD_EXECUTABLE(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${IMGUI_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${GLAD_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} imgui ${OPENGL_LIBRARIES} assimp ${GLAD_LIBRARY})
Sample error log:
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1535): undefined reference to `glfwSetCursor'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x154e): undefined reference to `glfwSetInputMode'
/usr/bin/ld: libimgui.a(imgui_impl_glfw.cpp.o): in function `ImGui_ImplGlfw_UpdateGamepads()':
imgui_impl_glfw.cpp:(.text+0x15e8): undefined reference to `glfwGetGamepadState'
/usr/bin/ld: libimgui.a(imgui_impl_glfw.cpp.o): in function `ImGui_ImplGlfw_NewFrame()':
imgui_impl_glfw.cpp:(.text+0x1bb5): undefined reference to `glfwGetWindowSize'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1bcf): undefined reference to `glfwGetFramebufferSize'
/usr/bin/ld: imgui_impl_glfw.cpp:(.text+0x1c4a): undefined reference to `glfwGetTime'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiStyle::ImGuiStyle()':
imgui.cpp:(.text+0xe23): undefined reference to `ImGui::StyleColorsDark(ImGuiStyle*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImBezierCubicClosestPoint(ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, int)':
imgui.cpp:(.text+0x2614): undefined reference to `ImBezierCubicCalc(ImVec2 const&, ImVec2 const&, ImVec2 const&, ImVec2 const&, float)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiTextFilter::Draw(char const*, float)':
imgui.cpp:(.text+0x564d): undefined reference to `ImGui::InputText(char const*, char*, unsigned long, int, int (*)(ImGuiInputTextCallbackData*), void*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiListClipper_SeekCursorAndSetupPrevLine(float, float)':
imgui.cpp:(.text+0x652f): undefined reference to `ImGui::TableEndRow(ImGuiTable*)'
/usr/bin/ld: libimgui.a(imgui.cpp.o): in function `ImGuiListClipper::Begin(int, float)':
I tried writing add_library instead of add_executable and make completed successfully. But then i don't have any executable output.
You are using imgui-plot which still requires ImGUI environment. ImGUI Plot Docs here. ImGUI example for GLFW3 and OpenGL3 here
You didn't link GLFW.
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} glfw3 imgui assimp ${GLAD_LIBRARY})
I want to add the library ixwebsocket my project. Following the instructions of the library I get the libixwebsocket.a (the compiled static library, correct me if I am wrong).
How I can use this .a file in my project?
For testing I have created the following program: main.cpp
#CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(cpptest VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(cpptest main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
target_link_libraries(cpptest "${CMAKE_SOURCE_DIR}/libixwebsocket.a")
//main.cpp
#include <iostream>
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
int main(int, char**) {
std::cout << "Hello, world!\n";
ix::WebSocket webSocket;
}
In the directory I have CMakeLists.txt, libixwebsocket.a and main.cpp.
I get a long list of undefined reference errors, the firsts ones:
[build] [ 50%] Linking CXX executable cpptest
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocket.cpp.o): in function `ix::WebSocket::start()':
[build] IXWebSocket.cpp:(.text+0x2240): undefined reference to `pthread_create'
ld] /usr/bin/ld: ../libixwebsocket.a(IXWebSocket.cpp.o): in function `ix::WebSocket::checkConnection(bool)':
[build] IXWebSocket.cpp:(.text+0x5b8d): undefined reference to `pthread_cond_clockwait'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `bool ix::WebSocketPerMessageDeflateCompressor::compressData<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) [clone .part.0]':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x67): undefined reference to `deflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateCompressor::~WebSocketPerMessageDeflateCompressor()':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x35c): undefined reference to `deflateEnd'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateCompressor::init(unsigned char, bool)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x3a6): undefined reference to `deflateInit2_'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::~WebSocketPerMessageDeflateDecompressor()':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x64c): undefined reference to `inflateEnd'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::init(unsigned char, bool)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x685): undefined reference to `inflateInit2_'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::decompress(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x7d6): undefined reference to `inflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `bool ix::WebSocketPerMessageDeflateCompressor::compressData<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<unsigned char, std::allocator<unsigned char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<unsigned char, std::allocator<unsigned char> >&) [clone .part.0]':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x90d): undefined reference to `deflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXSocketOpenSSL.cpp.o): in function `ix::SocketOpenSSL::openSSLInitialize()':
[build] IXSocketOpenSSL.cpp:(.text+0x1c): undefined reference to `OPENSSL_init_ssl'
[build] /usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x29): undefined reference to `OPENSSL_init_ssl'
How may these undefined refrence errors be resolved?
Thanks in advance!
It would be always best to use find_package() calls. This way you ensure that the library is indeed installed on your system, or is exported in the build tree.
If you have the library installed on your system you can use this call, like this:
find_package(ixwebsocket REQUIRED)
target_link_libraries(cpptest ixwebsocket::ixwebsocket)
In your case, since the library is compiled independently (and it is not a target in your tree), you need to use find_library(IXWEBSOCKET_LIB ixwebsocket) and specify additional hints/names if you have the library in your own location, following the docs.
I ran into a similar issue today, the library was linking just fine but a static lib is just a collection of .o files and has no understanding of it's own dependencies...
This command worked for me:
g++ -o output/myapp -std=c++17 myapp.cpp -Llib -framework CoreFoundation -framework Security -lmylib
I'm no expert in CMake but this answer seems to indicate that the below would achieve the same effect
target_link_libraries(program "-framework CoreFoundation" "-framework Security")
So if we apply it to your case:
#CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(cpptest VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(cpptest main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
target_link_libraries(cpptest "${CMAKE_SOURCE_DIR}/libixwebsocket.a" "-framework CoreFoundation")
I've got a feeling pthread_create is likely to live in CoreFoundation but I'm just guessing... You may need to try a few frameworks to find the right one.
Hope this helps a little :)
I couldn't compile my program which uses MagickWand with CMake. I'm getting this error:
[ 33%] Linking CXX executable bin/ScreenRecorder
/usr/bin/ld: CMakeFiles/ScreenRecorder.dir/source/main.cpp.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
main.cpp:(.text._ZNSt6threadC2IRFvvEJEvEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEvEEOT_DpOT0_]+0x20): undefined reference to `pthread_create'
/usr/bin/ld: CMakeFiles/ScreenRecorder.dir/source/screenshot.cpp.o: in function `printScrn(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
screenshot.cpp:(.text+0xd): undefined reference to `MagickWandGenesis'
/usr/bin/ld: screenshot.cpp:(.text+0x1a): undefined reference to `NewMagickWand'
/usr/bin/ld: screenshot.cpp:(.text+0x31): undefined reference to `MagickReadImage'
/usr/bin/ld: screenshot.cpp:(.text+0x4f): undefined reference to `MagickWriteImage'
/usr/bin/ld: screenshot.cpp:(.text+0x62): undefined reference to `DestroyMagickWand'
/usr/bin/ld: screenshot.cpp:(.text+0x6b): undefined reference to `MagickWandTerminus'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/ScreenRecorder.dir/build.make:99: bin/ScreenRecorder] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/ScreenRecorder.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(ScreenRecorder)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall")
set(source_dir "${PROJECT_SOURCE_DIR}/source/")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin/")
find_package(ImageMagick REQUIRED)
include_directories(
"/usr/include/ImageMagick-6/"
)
link_libraries(
"/usr/lib/x86_64-linux-gnu/"
)
file (GLOB src_files "${source_dir}*.cpp")
add_executable(ScreenRecorder ${src_files})
I tried using variables such as ${ImageMagick_MagickWand_INCLUDE_DIR} and ${ImageMagick_MagickWand_LIBS_DIR} but it says that "wand/MagickWand.h" not found.
Thanks for your answers.
I was able to resolve a similar issue by enclosing the auto-generated variables in quotes:
INCLUDE_DIRECTORIES(
"${ImageMagick_INCLUDE_DIRS}"
)
TARGET_LINK_LIBRARIES(MyProject
"${ImageMagick_LIBRARIES}"
)
The reason that this resolved the issue is that the variables are CMake lists.
Without quotes, they are concatenated to a long string:
MESSAGE(${ImageMagick_INCLUDE_DIRS})
MESSAGE(${ImageMagick_LIBRARIES})
->
1> [CMake] /usr/include/ImageMagick-6/usr/include/x86_64-linux-gnu/ImageMagick-6
1> [CMake] /usr/lib/x86_64-linux-gnu/libMagickWand-6.Q16.so
However, with quotes, the different elements of the list are separated using a semicolon:
MESSAGE("${ImageMagick_INCLUDE_DIRS}")
MESSAGE("${ImageMagick_LIBRARIES}")
->
1> [CMake] /usr/include/ImageMagick-6;/usr/include/x86_64-linux-gnu/ImageMagick-6
1> [CMake] /usr/lib/x86_64-linux-gnu/libMagickWand-6.Q16.so
\- lib
CMakeLists.txt
libA.a
libB.a
\- src
\- mod1
\- mod2
\- inc
\- mod1
\- mod2
\- libA
\- libB
\- examples
\- common_inc // common includes for all examples
\- my_executable
CMakeLists.txt
\- src
\- inc
src contains .cpp files for mod1 and mod2, both depend on libA, libB, inc contains headers for libA, libB, and for mod1 and mod2
The question is:
I want to make one shared library which contains libA.a, libB.a and also my src (mod1 and mod2), how I can achieve that?
Is there any other way besides creating static lib (lets call it libMy) from mod1 and mod2 and then link all 3 libs (libMy, libA, libB) into one shared library?
I came up with following CMakeLists.txt but it cannot find -llibA and -llibB
cmake_minimum_required (VERSION 3.5)
project (libMy CXX)
set (CMAKE_CXX_STANDARD 14)
set (SRC
"src/mod1/a1.cpp"
"src/mod2/a2.cpp"
)
set (INC
"inc/mod1/a1.hpp"
"inc/mod2/a2.hpp"
"inc/lib1/libA.hpp"
"inc/lib2/libB.hpp"
)
add_library(libMy STATIC ${SRC} ${INC})
target_include_directories(libMy PUBLIC inc)
add_library(master_library SHARED
dummy.cpp
)
target_link_libraries(master_library libMy)
target_link_libraries(master_library -lpthread)
target_link_libraries(master_library libA.a)
target_link_libraries(master_library libB.a)
UPDATE
Thanks to #Tsyvarev I managed to solve this problem. But there is another one...
When I try to link my shared library into my_executable I get undefined reference error, here is full error message:
../../../lib/libmylib.a(session.cpp.o): In function `mylib::network::session::write(mylib::network::packet const&)':
session.cpp:(.text+0x4e5): undefined reference to `libA::tcp_client::async_write(libA::tcp_client::write_request const&)'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::tcp_server()':
tcp_server.cpp:(.text+0x24b): undefined reference to `libA::tcp_server::tcp_server()'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::~tcp_server()':
tcp_server.cpp:(.text+0x323): undefined reference to `libA::tcp_server::~tcp_server()'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::start(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)::{lambda(std::shared_ptr<libA::tcp_client> const&)#1}::operator()(std::shared_ptr<libA::tcp_client> const&) const':
tcp_server.cpp:(.text+0x445): undefined reference to `libA::tcp_client::async_read(libA::tcp_client::read_request const&)'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::start(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
tcp_server.cpp:(.text+0x52c): undefined reference to `libA::tcp_server::start(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, std::function<bool (std::shared_ptr<libA::tcp_client> const&)> const&)'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::on_new_message(std::shared_ptr<libA::tcp_client> const&, libA::tcp_client::read_result const&)':
tcp_server.cpp:(.text+0x767): undefined reference to `libA::tcp_client::async_read(libA::tcp_client::read_request const&)'
tcp_server.cpp:(.text+0x801): undefined reference to `libA::tcp_client::disconnect(bool)'
../../../lib/libmylib.a(pool.cpp.o): In function `mylib::libB::pool::~pool()':
pool.cpp:(.text+0x42): undefined reference to `ConnectionPool_free'
pool.cpp:(.text+0x5f): undefined reference to `URL_free'
../../../lib/libmylib.a(pool.cpp.o): In function `mylib::libB::pool::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
pool.cpp:(.text+0x249): undefined reference to `URL_new'
pool.cpp:(.text+0x26a): undefined reference to `ConnectionPool_new'
pool.cpp:(.text+0x284): undefined reference to `Exception_stack'
pool.cpp:(.text+0x28b): undefined reference to `pthread_getspecific'
pool.cpp:(.text+0x298): undefined reference to `Exception_stack'
pool.cpp:(.text+0x2a9): undefined reference to `pthread_setspecific'
pool.cpp:(.text+0x2e3): undefined reference to `ConnectionPool_start'
pool.cpp:(.text+0x2f8): undefined reference to `Exception_stack'
pool.cpp:(.text+0x2ff): undefined reference to `pthread_getspecific'
pool.cpp:(.text+0x30c): undefined reference to `Exception_stack'
pool.cpp:(.text+0x316): undefined reference to `pthread_setspecific'
pool.cpp:(.text+0x325): undefined reference to `SQLException'
pool.cpp:(.text+0x39d): undefined reference to `Exception_throw'
../../../lib/libmylib.a(connection.cpp.o): In function `mylib::libB::connection::connection(ConnectionPool_S*)':
connection.cpp:(.text+0x18): undefined reference to `ConnectionPool_getConnection'
../../../lib/libmylib.a(connection.cpp.o): In function `mylib::libB::connection::~connection()':
connection.cpp:(.text+0x41): undefined reference to `Connection_close'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::query(Connection_S*, char const*)':
query.cpp:(.text+0x28): undefined reference to `Connection_prepareStatement'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::set(int)':
query.cpp:(.text+0x93): undefined reference to `PreparedStatement_setInt'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::set(char const*)':
query.cpp:(.text+0xe3): undefined reference to `PreparedStatement_setString'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::get_int()':
query.cpp:(.text+0x15d): undefined reference to `ResultSet_getInt'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::get_str[abi:cxx11]()':
query.cpp:(.text+0x1ee): undefined reference to `ResultSet_getString'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::execute_query()':
query.cpp:(.text+0x28e): undefined reference to `PreparedStatement_executeQuery'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::execute()':
query.cpp:(.text+0x2e4): undefined reference to `PreparedStatement_execute'
query.cpp:(.text+0x2f3): undefined reference to `PreparedStatement_rowsChanged'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::next()':
query.cpp:(.text+0x326): undefined reference to `ResultSet_next'
collect2: error: ld returned 1 exit status
CMakeFiles/my_executable.dir/build.make:121: recipe for target 'my_executable' failed
make[2]: *** [my_executable] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/my_executable.dir/all' failed
make[1]: *** [CMakeFiles/my_executable.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
/lib/CMakeLists.txt:
cmake_minimum_required (VERSION 3.5)
project (my_executable CXX)
set (CMAKE_CXX_STANDARD 14)
set (SRC
"src/main.cpp"
"src/mysrcA.cpp"
)
set (INC
"inc/mysrcA.hpp"
)
add_library(mylib STATIC IMPORTED GLOBAL)
set_target_properties(mylib PROPERTIES
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/../../lib/mylib.so"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/../../lib/inc"
)
add_executable (my_executable ${SRC})
target_include_directories(dbserver PRIVATE "${CMAKE_SOURCE_DIR}/inc")
target_include_directories(dbserver PRIVATE "${CMAKE_SOURCE_DIR}/../common_inc")
target_link_libraries(my_executable mylib)
/examples/my_executable/CMakeLists.txt:
cmake_minimum_required (VERSION 3.5)
project (mylib CXX)
set (CMAKE_CXX_STANDARD 14)
set (SRC
"src/mod1/mod1A.cpp"
"src/mod2/mod2A.cpp"
)
set (INC
"inc/mod1/mod1A.hpp"
"inc/mod2/mod2A.hpp"
"inc/libA/libA.hpp"
"inc/libB/libB.h"
)
add_library(mylib STATIC ${SRC} ${INC})
target_include_directories(mylib PUBLIC ${CMAKE_SOURCE_DIR}/inc)
add_library(libA STATIC IMPORTED)
set_target_properties(libA PROPERTIES
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/libA.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/inc/libA"
)
add_library(libB STATIC IMPORTED)
set_target_properties(libB PROPERTIES
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/res/libB.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/inc/libB.h"
)
#)
target_link_libraries(mylib libA libB -lpthread)
When I'm compiling the Triangle project from the opengl superbible 5th ed I've this code errors
> /home/fpointbin/projects/Triangle/build> make
[ 7%] Building CXX object CMakeFiles/gltools.dir/src/GLBatch.cpp.o
[ 15%] Building CXX object CMakeFiles/gltools.dir/src/GLShaderManager.cpp.o
[ 23%] Building CXX object CMakeFiles/gltools.dir/src/GLTools.cpp.o
[ 30%] Building CXX object CMakeFiles/gltools.dir/src/GLTriangleBatch.cpp.o
[ 38%] Building CXX object CMakeFiles/gltools.dir/src/math3d.cpp.o
[ 46%] Building C object CMakeFiles/gltools.dir/src/glew.c.o
Linking CXX shared library libgltools.so
[ 46%] Built target gltools
Scanning dependencies of target gltools-static
[ 53%] Building CXX object CMakeFiles/gltools-static.dir/src/GLBatch.cpp.o
[ 61%] Building CXX object CMakeFiles/gltools-static.dir/src/GLShaderManager.cpp.o
[ 69%] Building CXX object CMakeFiles/gltools-static.dir/src/GLTools.cpp.o
[ 76%] Building CXX object CMakeFiles/gltools-static.dir/src/GLTriangleBatch.cpp.o
[ 84%] Building CXX object CMakeFiles/gltools-static.dir/src/math3d.cpp.o
[ 92%] Building C object CMakeFiles/gltools-static.dir/src/glew.c.o
Linking CXX static library libgltools.a
[ 92%] Built target gltools-static
Linking CXX executable triangle
CMakeFiles/triangle.dir/main.cpp.o: In function `ChangeSize(int, int)':
/home/fpointbin/projects/Triangle/main.cpp:22: undefined reference to `glViewport'
CMakeFiles/triangle.dir/main.cpp.o: In function `SetupRC()':
/home/fpointbin/projects/Triangle/main.cpp:32: undefined reference to `glClearColor'
/home/fpointbin/projects/Triangle/main.cpp:34: undefined reference to `GLShaderManager::InitializeStockShaders()'
/home/fpointbin/projects/Triangle/main.cpp:41: undefined reference to `GLBatch::Begin(unsigned int, unsigned int, unsigned int)'
/home/fpointbin/projects/Triangle/main.cpp:43: undefined reference to `GLBatch::End()'
CMakeFiles/triangle.dir/main.cpp.o: In function `RenderScene()':
/home/fpointbin/projects/Triangle/main.cpp:53: undefined reference to `glClear'
/home/fpointbin/projects/Triangle/main.cpp:56: undefined reference to `GLShaderManager::UseStockShader(GLT_STOCK_SHADER, ...)'
/home/fpointbin/projects/Triangle/main.cpp:57: undefined reference to `GLBatch::Draw()'
/home/fpointbin/projects/Triangle/main.cpp:60: undefined reference to `glutSwapBuffers'
CMakeFiles/triangle.dir/main.cpp.o: In function `main':
/home/fpointbin/projects/Triangle/main.cpp:68: undefined reference to `gltSetWorkingDirectory(char const*)'
/home/fpointbin/projects/Triangle/main.cpp:70: undefined reference to `glutInit'
/home/fpointbin/projects/Triangle/main.cpp:71: undefined reference to `glutInitDisplayMode'
/home/fpointbin/projects/Triangle/main.cpp:72: undefined reference to `glutInitWindowSize'
/home/fpointbin/projects/Triangle/main.cpp:73: undefined reference to `glutCreateWindow'
/home/fpointbin/projects/Triangle/main.cpp:74: undefined reference to `glutReshapeFunc'
/home/fpointbin/projects/Triangle/main.cpp:75: undefined reference to `glutDisplayFunc'
/home/fpointbin/projects/Triangle/main.cpp:77: undefined reference to `glewInit'
/home/fpointbin/projects/Triangle/main.cpp:79: undefined reference to `glewGetErrorString'
/home/fpointbin/projects/Triangle/main.cpp:85: undefined reference to `glutMainLoop'
CMakeFiles/triangle.dir/main.cpp.o: In function `__static_initialization_and_destruction_0':
/home/fpointbin/projects/Triangle/main.cpp:14: undefined reference to `GLBatch::GLBatch()'
/home/fpointbin/projects/Triangle/main.cpp:14: undefined reference to `GLBatch::~GLBatch()'
/home/fpointbin/projects/Triangle/main.cpp:15: undefined reference to `GLShaderManager::GLShaderManager()'
/home/fpointbin/projects/Triangle/main.cpp:15: undefined reference to `GLShaderManager::~GLShaderManager()'
CMakeFiles/triangle.dir/main.cpp.o: In function `GLBatch::CopyVertexData3f(float*)':
/home/fpointbin/projects/Triangle/include/GLBatch.h:89: undefined reference to `GLBatch::CopyVertexData3f(float (*) [3])'
collect2: ld returned 1 exit status
make[2]: *** [triangle] Error 1
make[1]: *** [CMakeFiles/triangle.dir/all] Error 2
make: *** [all] Error 2
*** Failed ***
This is my Cmakelist.txt:
cmake_minimum_required (VERSION 2.6)
project(triangle)
find_package(X11)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_library(M_LIBRARY m)
find_library(GLEW_LIBRARY GLEW)
set ( INCLUDE_DIRS
"${CMAKE_SOURCE_DIR}/include"
${INCLUDE_DIRS}
)
include_directories (
${INCLUDE_DIRS}
)
if(UNIX)
set ( INCLUDE_DIRS
"/usr/include"
"/usr/local/include"
"/usr/include/GL"
${INCLUDE_DIRS}
)
endif(UNIX)
set ( GLTOOLS_HDRS
"${CMAKE_SOURCE_DIR}/include/GLBatchBase.h"
"${CMAKE_SOURCE_DIR}/include/GLBatch.h"
"${CMAKE_SOURCE_DIR}/include/GLFrame.h"
"${CMAKE_SOURCE_DIR}/include/GLFrustum.h"
"${CMAKE_SOURCE_DIR}/include/GLGeometryTransform.h"
"${CMAKE_SOURCE_DIR}/include/GLMatrixStack.h"
"${CMAKE_SOURCE_DIR}/include/GLShaderManager.h"
"${CMAKE_SOURCE_DIR}/include/GLTools.h"
"${CMAKE_SOURCE_DIR}/include/GLTriangleBatch.h"
"${CMAKE_SOURCE_DIR}/include/math3d.h"
"${CMAKE_SOURCE_DIR}/include/StopWatch.h"
"${CMAKE_SOURCE_DIR}/include/glew.h"
)
set ( GLTOOLS_SRCS
"${CMAKE_SOURCE_DIR}/src/GLBatch.cpp"
"${CMAKE_SOURCE_DIR}/src/GLShaderManager.cpp"
"${CMAKE_SOURCE_DIR}/src/GLTools.cpp"
"${CMAKE_SOURCE_DIR}/src/GLTriangleBatch.cpp"
"${CMAKE_SOURCE_DIR}/src/math3d.cpp"
"${CMAKE_SOURCE_DIR}/src/glew.c"
)
add_library ( gltools-static ${GLTOOLS_SRCS})
add_library ( gltools SHARED ${GLTOOLS_SRCS})
target_link_libraries (gltools ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${GLEW_LIBRARY} ${M_LIBRARY} ${X11_LIBRARIES})
target_link_libraries (gltools-static ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${GLEW_LIBRARY} ${M_LIBRARY} ${X11_LIBRARIES})
set_target_properties(gltools-static PROPERTIES OUTPUT_NAME gltools)
add_executable(triangle main.cpp)
What's the problem?
You are right, the errors are not from the compiler, they are from the linker. Since a lot of the undefined references are about glut functions, first verify that you can link glut properly. Have a look at this link - how to set up Glut.
Most likely you didn't include a header or the include directive in the makefile is wrong because the errors are pretty clear to me: the compiler can't find the core function of OpenGL which are included in the OpenGL headers.