How to setup a C++ project with MySQL Connector and Make - c++

despite having developed applications in C++ for about two to three years, I never had to setup a project on my own. Most of the project were preconfigured thus I never learned to do it myself. Having lecture-free time at the moment, I thought to myself: "I am going to create my first own CMake C++ project".
Because I knew I wanted to store information in a database,I set off to create a simple CMake project with one executable linked to the MySQL Connector for C++, and instantly failed...
Since I - oddly - could not find helpful information anywhere else, I hope one of you could be my savior.
My project is setup as follows:
Root
- CMakeLists.txt
- build
- src
- tests
-- main.cpp
- include
-- mysql_connection.h
-- cppconn
--- driver.h
--- exception.h
--- resultset.h
--- statement.h
--- ...
-- ...
- libs
-- libmysqlcppconn.dylib -> libmysqlcppconn.7.8.0.12.dylib (symlink)
-- libmysqlcppconn.7.8.0.12.dylib
-- libmysqlcppconn8.1.8.0.12.dylib
-- libmysqlcppconn8.1.dylib -> libmysqlcppconn8.1.8.0.12.dylib (symlink)
-- libmysqlcppconn8.dylib -> libmysqlcppconn8.1.8.0.12.dylib (symlink)
-- libssl.dylib
-- libcrypto.dylib
-- ...
My main.cpp contains:
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
int main(int argc, char const *argv[])
{
sql::Driver *driver;
sql::Connection *con;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306","root","securepw");
return 0;
}
And my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(EconSim)
add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
target_include_directories(EconSim PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(EconSim ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn.dylib)
target_compile_features(EconSim PUBLIC cxx_std_17)
I am able to compile the application but I get the following error when executing it:
dyld: Library not loaded: libmysqlcppconn.7.dylib
Referenced from: /Users/aosterthun/Documents/Programming/EconSim/build/./EconSim
Reason: image not found
Abort trap: 6
When instead using libmysqlcppconn8.dylib in the CMakeLists.txt:
And my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(EconSim)
add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
target_include_directories(EconSim PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(EconSim ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn8.dylib)
target_compile_features(EconSim PUBLIC cxx_std_17)
I get a compile error:
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/aosterthun/Documents/Programming/EconSim/build --config Debug --target all -- -j 6
[build] [ 50%] Linking CXX executable EconSim
[build] Undefined symbols for architecture x86_64:
[build] "_get_driver_instance", referenced from:
[build] _main in main.cpp.o
[build] ld: symbol(s) not found for architecture x86_64
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[build] make[2]: *** [EconSim] Error 1
[build] make[1]: *** [CMakeFiles/EconSim.dir/all] Error 2
[build] make: *** [all] Error 2
[build] Build finished with exit code 2
I found this: How do I link C++ MySQL Connector Libraries to Cmake? but that sadly did not solve my problem.
I also found CLion: undefined “_get_driver_instance” which resulted in this CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(EconSim)
include_directories(${PROJECT_SOURCE_DIR}/include)
add_library(libmysqlcppconn STATIC IMPORTED)
add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn-static.a)
target_compile_features(EconSim PUBLIC cxx_std_17)
target_link_libraries(EconSim libmysqlcppconn)
Which then results in this error:
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/aosterthun/Documents/Programming/EconSim/build --config Debug --target all -- -j 6
[build] [ 50%] Linking CXX executable EconSim
[build] Undefined symbols for architecture x86_64:
[build] "_BIO_free", referenced from:
[build] sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build] caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build] "_BIO_new_bio_pair", referenced from:
[build] dummy_function_needed_by_xplugin() in libmysqlcppconn-static.a(my_aes_openssl.cc.o)
[build] "_BIO_new_mem_buf", referenced from:
[build] sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build] caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build] "_BN_bin2bn", refer...
I would appreciate any help. Even a hint to an obvious solution I have not found. I am still a bit confused why I did not find helpful information on this topic. Cause there should be lots of people developing with this technology.
Just ask for further information if I missed any.

You need install openssl and link libraries libcrypto, libssl, libresolv.
I solve this problem as follows:
target_link_libraries(${PROJECT_NAME}
PUBLIC
mysqlclient
mysqlcppconn-static
crypto
ssl
resolv
)

Related

Undefined symbols for architecture x86_64 mac os library Clion error

I am trying to build my library and link it to the project. But I get an error. The compiler does not see the implementation of the F::method(); function in lesson6.cpp. Why? Can anyone explain what the error is?
Error
====================[ Build | lesson6 | Debug ]=================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/mbp15/CLionProjects/lesson6/cmake-build-debug --target lesson6 -- -j 6
Scanning dependencies of target lesson6
[ 50%] Building CXX object CMakeFiles/lesson6.dir/app/main.cpp.o
[100%] Linking CXX executable lesson6
Undefined symbols for architecture x86_64:
"F::method(int, int)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [lesson6] Error 1
make[2]: *** [CMakeFiles/lesson6.dir/all] Error 2
make[1]: *** [CMakeFiles/lesson6.dir/rule] Error 2
make: *** [lesson6] Error 2
Cmake.h
cmake_minimum_required(VERSION 3.17)
project(lesson6 VERSION 1.0.0 DESCRIPTION "Lesson project" LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
add_executable(lesson6 app/main.cpp)
add_library(MyLibrary include/lesson6.h src/lesson6.cpp)
target_link_libraries(lesson6 PUBLIC ${MyLibrary})
main.cpp
#include "../include/lesson6.h"
int main(){
F object;
object.method(1,2);
return 0;
}
lesson6.h
#ifndef LESSON6_LESSON6_H
#define LESSON6_LESSON6_H
class F {
public:
int method(int one, int two);
};
#endif //LESSON6_LESSON6_H
lesson6.cpp
#include "../include/lesson6.h"
int F::method(int one, int two) {
return one + two;
}
Project tree
You don't need to dereference the library target in your CMakeLists.txt.
Try changing the last line to
target_link_libraries(lesson6 PUBLIC MyLibrary)

Compiling OpenCV project using CMake results in build errors while regular compilation works fine

I downloaded CMake and OpenCV using homebrew, I am trying to run a simple openCV program that opens my camera and starts to stream a video. I can compile my program regularly using the command line with:
clang++ -std=c++11 lib_cpp.cpp -o execute 'pkg-config --cflags --libs opencv4'
However when I try to build and compile my project using CMake I am running into trouble and errors.
Script:
#!/bin/bash
SDK=/Users/David/Library/Android/sdk
CMAKE=$SDK/cmake/3.18.1/bin/cmake
NDK=$SDK/ndk/23.0.7272597
TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake
TOOLCHAIN_BINARIES=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin
LD=$TOOLCHAIN_BINARIES/ld
ABI=arm64-v8a
MIN_SDK=16
STL=c++_shared
STD=-std=c++11 -o
CXX=$TOOLCHAIN_BINARIES/aarch64-linux-android30-clang++
CMAKE_ARGS=""
export CXX=$CXX
$CMAKE -DCMAKE_CXX_FLAGS=$STD -DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE -DANDROID_ABI=$ABI -DANDROID_NATIVE_API_LEVEL=$MIN_SDK -DANDROID_STL=$STL $CMAKE_ARGS
CMakeLists.txt:
cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
project(shared_library_cpp VERSION 1.0.0 LANGUAGES CXX)
set(OpenCV_DIR "/usr/local/Cellar/opencv/4.5.2_1/lib/cmake/opencv4")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_library(shared_library_cpp SHARED lib_cpp.cpp cpp.def)
add_executable(cpp_exec lib_cpp.cpp)
target_link_libraries( shared_library_cpp ${OpenCV_LIBS} )
set_target_properties(shared_library_cpp PROPERTIES
PUBLIC_HEADER lib_cpp.h
VERSION ${PROJECT_VERSION}
SOVERSION 1
OUTPUT_NAME "cpp"
XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "MacOS_ID"
)
For some reason I am getting a linkage problem, the script runs smoothly but when running make I am getting an error in the middle of the build
script output:
./create_makefile.sh: line 12: -o: command not found
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.
CMake Warning at /Users/David/Library/Android/sdk/ndk/23.0.7272597/build/cmake/android-legacy.toolchain.cmake:416 (message):
An old version of CMake is being used that cannot automatically detect
compiler attributes. Compiler identification is being bypassed. Some
values may be wrong or missing. Update to CMake 3.19 or newer to use
CMake's built-in compiler identification.
Call Stack (most recent call first):
/Users/David/Library/Android/sdk/ndk/23.0.7272597/build/cmake/android.toolchain.cmake:55 (include)
/Users/David/Library/Android/sdk/cmake/3.18.1/share/cmake-3.18/Modules/CMakeDetermineSystem.cmake:93 (include)
CMakeLists.txt:3 (project)
-- Detecting CXX compiler ABI info
CMake Warning at /Users/David/Library/Android/sdk/ndk/23.0.7272597/build/cmake/android-legacy.toolchain.cmake:416 (message):
An old version of CMake is being used that cannot automatically detect
compiler attributes. Compiler identification is being bypassed. Some
values may be wrong or missing. Update to CMake 3.19 or newer to use
CMake's built-in compiler identification.
Call Stack (most recent call first):
/Users/David/Library/Android/sdk/ndk/23.0.7272597/build/cmake/android.toolchain.cmake:55 (include)
/Users/David/Desktop/CPP/CMakeFiles/3.18.1-g262b901/CMakeSystem.cmake:6 (include)
/Users/David/Desktop/CPP/CMakeFiles/CMakeTmp/CMakeLists.txt:2 (project)
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Users/David/Library/Android/sdk/ndk/23.0.7272597/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local/Cellar/opencv/4.5.2_1 (found version "4.5.2")
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/David/Desktop/CPP
error output:
Scanning dependencies of target cpp_exec
[ 25%] Building CXX object CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o
[ 50%] Linking CXX executable cpp_exec
ld: error: undefined symbol: cv::VideoCapture::VideoCapture(int, int)
>>> referenced by lib_cpp.cpp:22
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
ld: error: undefined symbol: cv::VideoCapture::isOpened() const
>>> referenced by lib_cpp.cpp:23
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
ld: error: undefined symbol: cv::Mat::Mat()
>>> referenced by lib_cpp.cpp:27
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
ld: error: undefined symbol: cv::VideoCapture::operator>>(cv::Mat&)
>>> referenced by lib_cpp.cpp:28
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
ld: error: undefined symbol: cv::imshow(std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, cv::_InputArray const&)
>>> referenced by lib_cpp.cpp:29
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
ld: error: undefined symbol: cv::waitKey(int)
>>> referenced by lib_cpp.cpp:31
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
ld: error: undefined symbol: cv::Mat::~Mat()
>>> referenced by lib_cpp.cpp:33
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
>>> referenced by lib_cpp.cpp:33
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
ld: error: undefined symbol: cv::VideoCapture::~VideoCapture()
>>> referenced by lib_cpp.cpp:36
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
>>> referenced by lib_cpp.cpp:36
>>> CMakeFiles/cpp_exec.dir/lib_cpp.cpp.o:(startVideo)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cpp_exec] Error 1
make[1]: *** [CMakeFiles/cpp_exec.dir/all] Error 2
make: *** [all] Error 2
After some digging I found the solution to this problem. I was trying to compile my program for arm64 but the dylibs were compiled for x86-64. Downloading the sources from OpenCV, and giving the path to arm64 libraries solved this.

CMake - undefined symbols encountered when building application depends on PcapPlusPlus

I programmed a simple application using PcapPlusPlus library with CLion on MacOS.
The code is really simple:
#include "../include/PCPP/PcapFileDevice.h"
int main(int argc, char* argv[])
{
std::string path;
pcpp::PcapFileReaderDevice reader(path);
reader.open();
reader.close();
return 0;
}
Here is CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
project (Test CXX)
add_executable(cmd_main ${PROJECT_SOURCE_DIR}/test/cmd_main.cpp)
target_include_directories(cmd_main
PUBLIC
${PROJECT_SOURCE_DIR}/include)
target_link_libraries(cmd_main
PUBLIC
${PROJECT_SOURCE_DIR}/lib/libCommon++.a
${PROJECT_SOURCE_DIR}/lib/libPacket++.a
${PROJECT_SOURCE_DIR}/lib/libPcap++.a
)
The directory is also simple:
|
|--include
|----PCPP/
|--lib
|----libCommon++.a
|----libPacket++.a
|----libPcap++.a
|--test
|----cmd_main.cpp
Here is compile result:
(base) 2ir0#iMac Test % cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/2ir0/Documents/Maltrace
(base) 2ir0#iMac Test % make
Scanning dependencies of target cmd_main
[ 50%] Building CXX object CMakeFiles/cmd_main.dir/test/cmd_main.cpp.o
[100%] Linking CXX executable cmd_main
Undefined symbols for architecture x86_64:
"_pcap_close", referenced from:
pcpp::IFileDevice::close() in libPcap++.a(PcapFileDevice.o)
pcpp::PcapFileReaderDevice::open() in libPcap++.a(PcapFileDevice.o)
"_pcap_compile", referenced from:
pcpp::IPcapDevice::setFilter(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in libPcap++.a(PcapDevice.o)
......
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cmd_main] Error 1
make[1]: *** [CMakeFiles/cmd_main.dir/all] Error 2
make: *** [all] Error 2
It seems cmd_main didn't link PcapPlusPlus library. But why?
I read the error information carefully, found that PcapPlusPlus need libpcap library, so I added libpcap.a, and fixed the problem.

How to fix "ld: symbol(s) not found for architecture x86_64" using C++, CMake and Tensorflow?

I created a test project in C ++ using TensorFlow with CMake. But I have an error:
ld: symbol(s) not found for architecture x86_64
I think, I have an error in my CMake files. When I try compile via the terminal with gcc tensortest.cpp -ltensorflow -o tf, everything works fine.
I have two CMake files.
FindTensorFlow.cmake:
# Locates the tensorFlow library and include directories.
include(FindPackageHandleStandardArgs)
unset(TENSORFLOW_FOUND)
find_path(TensorFlow_INCLUDE_DIR
NAMES
tensorflow
HINTS
/usr/local/include/tensorflow)
find_library(TensorFlow_LIBRARY
NAMES
libtensorflow_framework.1.14.0.dylib
libtensorflow_framework.1.dylib
libtensorflow_framework.dylib
libtensorflow.1.14.0.dylib
libtensorflow.1.dylib
libtensorflow.dylib
HINTS
/usr/local/lib)
# set TensorFlow_FOUND
find_package_handle_standard_args(TensorFlow DEFAULT_MSG TensorFlow_INCLUDE_DIR TensorFlow_LIBRARY)
# set external variables for usage in CMakeLists.txt
if(TENSORFLOW_FOUND)
set(TensorFlow_LIBRARIES ${TensorFlow_LIBRARY})
set(TensorFlow_INCLUDE_DIRS ${TensorFlow_INCLUDE_DIR})
endif()
# hide locals from GUI
mark_as_advanced(TensorFlow_INCLUDE_DIR TensorFlow_LIBRARY)
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(TensorFlowTest)
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
find_package(TensorFlow REQUIRED)
include_directories(${TensorFlow_INCLUDE_DIRS} ${TensorFlow_LIBRARIES})
add_executable(TensorFlowTest main.cpp)
target_link_libraries(TensorFlowTest ${TensorFlow_INCLUDE_DIRS} ${TensorFlow_LIBRARIES})
And one main.cpp
#include <stdio.h>
#include <tensorflow/c/c_api.h>
int main() {
printf("Hello from TensorFlow C library version %s\n", TF_Version());
return 0;
}
When reload project, I have a CMake message:
-- Found TensorFlow: /usr/local/include
-- Configuring done
WARNING: Target "TensorFlowTest" requests linking to directory "/usr/local/include". Targets may link only to libraries. CMake is dropping the item.
-- Generating done
-- Build files have been written to: /Users/neikr/CLionProjects/TensorFlowTest/cmake-build-debug
And when compiling:
Scanning dependencies of target TensorFlowTest
[ 50%] Building CXX object CMakeFiles/TensorFlowTest.dir/main.cpp.o
[100%] Linking CXX executable TensorFlowTest
Undefined symbols for architecture x86_64:
"_TF_Version", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [TensorFlowTest] Error 1
make[2]: *** [CMakeFiles/TensorFlowTest.dir/all] Error 2
make[1]: *** [CMakeFiles/TensorFlowTest.dir/rule] Error 2
make: *** [TensorFlowTest] Error 2
Update
I tried make with VERBOSE=1 and I get this output:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -S/Users/neikr/CLionProjects/TensorFlowTest -B/Users/neikr/CLionProjects/TensorFlowTest --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_progress_start /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/TensorFlowTest.dir/build.make CMakeFiles/TensorFlowTest.dir/depend
cd /Users/neikr/CLionProjects/TensorFlowTest && /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_depends "Unix Makefiles" /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles/TensorFlowTest.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/TensorFlowTest.dir/build.make CMakeFiles/TensorFlowTest.dir/build
[ 50%] Linking CXX executable TensorFlowTest
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/TensorFlowTest.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/TensorFlowTest.dir/main.cpp.o -o TensorFlowTest -Wl,-rpath,/usr/local/lib /usr/local/lib/libtensorflow_framework.1.14.0.dylib
Undefined symbols for architecture x86_64:
"_TF_Version", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [TensorFlowTest] Error 1
make[1]: *** [CMakeFiles/TensorFlowTest.dir/all] Error 2
make: *** [all] Error 2
Also, I tried adding message("TensorFlow_LIBRARIES: ${TensorFlow_LIBRARIES}") to the end of my CMake and I get this message:
TensorFlow_LIBRARIES: /usr/local/lib/libtensorflow_framework.1.14.0.dylib
Based on your print-out from the message() command, it is more clear what is going on. In your FindTensorFlow.cmake file, the find_library() call only finds one library (libtensorflow_framework.1.14.0.dylib), which is expected behavior:
Once one of the calls succeeds the result variable will be set and stored in the cache so that no call will search again.
When more than one value is given to the NAMES option this command by default will consider one name at a time and search every directory for it.
Thus, once find_library() finds the libtensorflow_framework library, the search stops. If you want to also find libtensorflow.dylib, which may help with your link error, you have to specify another find_library() call, using a different TensorFlow_ variable. So changing that section of your FindTensorFlow.cmake to something like this should help:
find_library(TensorFlow_LIBRARY
NAMES
libtensorflow.1.14.0.dylib
libtensorflow.1.dylib
libtensorflow.dylib
HINTS
/usr/local/lib)
find_library(TensorFlow_FRAMEWORK_LIBRARY
NAMES
libtensorflow_framework.1.14.0.dylib
libtensorflow_framework.1.dylib
libtensorflow_framework.dylib
HINTS
/usr/local/lib)
# set TensorFlow_FOUND
find_package_handle_standard_args(TensorFlow DEFAULT_MSG
TensorFlow_INCLUDE_DIR
TensorFlow_LIBRARY
TensorFlow_FRAMEWORK_LIBRARY
)

Minimal example with GLFW fails because of undefined symbols for architecture x86_64

I am trying to compile the following minimal example for GLFW:
#include <GLFW/glfw3.h>
#include <thread>
int main() {
glfwInit();
std::this_thread::sleep_for(std::chrono::seconds(1));
glfwTerminate();
return 0;
}
My CMakeLists.txt file is:
cmake_minimum_required(VERSION 3.9)
project(viewer)
set(CMAKE_CXX_STANDARD 11)
find_package(glfw3 3.2 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
add_executable(viewer main.cpp)
target_link_libraries(viewer ${GLFW_LIBRARIES})
If I try to compile the code, it fails with the following error message:
[ 50%] Linking CXX executable visualiser
Undefined symbols for architecture x86_64:
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [visualiser] Error 1
make[2]: *** [CMakeFiles/visualiser.dir/all] Error 2
make[1]: *** [CMakeFiles/visualiser.dir/rule] Error 2
make: *** [visualiser] Error 2
I see the similar questions, e.g.:
Trouble compiling GLFW, undefined symbols
http://dudandan.com/2017/02/15/Setup-glfw-and-glew/
But their solutions did not really help.
UPD: Following the comment of #thomas_f, I modified my CMakeLists.txt file as follows:
cmake_minimum_required(VERSION 3.9)
project(viewer)
set(CMAKE_CXX_STANDARD 11)
find_package(glfw3 3.2 REQUIRED)
include_directories(${GLFW3_INCLUDE_DIR})
add_executable(viewer main.cpp)
target_link_libraries(viewer ${GLFW3_LIBRARY})
message(GLFW LIB: ${GLFW3_LIBRARY})
I also made sure that there is no CMakeCache.txt in my build directory:
$ ls -a
. .. .idea CMakeLists.txt cmake-build-debug main.cpp
However, I still get the same error message. It seems, that
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" ........./viewer
GLFWLIB:
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/denis/Documents/projects/theia/code/visualiser/cmake-build-debug
[Finished]
So it seems that ${GLFW3_LIBRARY} is not defined.
From glfw3Config.cmake:
# - Config file for the glfw3 package
# It defines the following variables
# GLFW3_INCLUDE_DIR, the path where GLFW headers are located
# GLFW3_LIBRARY_DIR, folder in which the GLFW library is located
# GLFW3_LIBRARY, library to link against to use GLFW
It seems that you are referencing the wrong variables. Also, there is no need to invoke include_directories() in this case, since GLFW is obviously installed in a standard location, i.e. your compiler already knows where to find it.
Edit: I was able to reproduce the linker error and changing the variable name to GLFW3_LIBRARY fixed it.
Clarification: Change your link command to: target_link_libraries(viewer ${GLFW3_LIBRARY})
Update if you're on Mac OS: If you're experiencing the same issues as OP, it may be because glfw3Config.cmake does not export the variables mentioned in my answer. Instead it creates an imported library, glfw. In this case, the correct way to link to glfw would be to simply do this: target_link_libraries(<target> glfw).
If glfw was installed using brew, you should find the .cmake-files under: /usr/local/Cellar/glfw/<version>/lib/cmake/glfw3.