I'm trying to link libsqlite3.dylib with C++ using CMake, but I keep getting the following error:
Undefined symbols for architecture x86_64:
"_sqlite3_close", referenced from:
OpnavCamera::~OpnavCamera() in opnav_camera.cpp.o
OpnavCamera::updateState() in opnav_camera.cpp.o
"_sqlite3_exec", referenced from:
select_stmt(char const*) in opnav_camera.cpp.o
"_sqlite3_open", referenced from:
OpnavCamera::updateState() in opnav_camera.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]: *** [../modules/opnav_camera/_opnav_camera.so] Error 1
make[1]: *** [SimCode/CMakeFiles/_opnav_camera.dir/all] Error 2
make: *** [all] Error 2
Looking around online, it looks like I'm not successfully linking the library. I'm new to CMake, so I have no idea what I'm doing.
I've added the following to my CMakelists.txt:
#SQLite 3
# Look for the header file.
FIND_PATH(SQLITE3_INCLUDE_DIR NAMES sqlite3.h)
# Look for the library.
FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite3.0)
# Handle the QUIETLY and REQUIRED arguments and set SQLITE3_FOUND to TRUE if all listed variables are TRUE.
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR)
# Copy the results to the output variables.
IF(SQLITE3_FOUND)
SET(SQLITE3_LIBRARIES ${SQLITE3_LIBRARY})
SET(SQLITE3_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIR})
message( " Found ")
ELSE(SQLITE3_FOUND)
SET(SQLITE3_LIBRARIES)
SET(SQLITE3_INCLUDE_DIRS)
ENDIF(SQLITE3_FOUND)
set (CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
MARK_AS_ADVANCED(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES)
CMake is finding the library, but I think I'm just missing a step in how to actually link it.
enter image description here
What piece am I missing?
ld: symbol(s) not found for architecture x86_64
It appears you are building a 64 bit application and attempting to link against a 32 bit library. You will need to link against the 64 bit version of the library instead.
Related
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.
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.
while building my Qt project, an iOS app, I'm getting a symbols not found error for a missing architecture (i386).
The project uses subdirs which again contain 2 self written libraries and an app which links to them.
After some investigating I found out that my libraries use armv7 and arm64 while my app uses i386. What I'm getting from this is that the linker doesn't seem to find the libraries with their respective architectures.
This is what I get from the compiler while building:
ld: warning: ld: warning: ignoring file /Users/me/myprojects/build-myproject-iphonesimulator_clang_Qt_5_7_1_for_iOS-Release/install/lib/libwebdav.a, missing required architecture i386 in file /Users/me/myprojects/build-myproject-iphonesimulator_clang_Qt_5_7_1_for_iOS-Release/install/lib/libwebdav.a (2 slices)ignoring file /Users/me/myprojects/build-myproject-iphonesimulator_clang_Qt_5_7_1_for_iOS-Release/install/lib/libcommon.a, missing required architecture i386 in file /Users/me/myprojects/build-myproject-iphonesimulator_clang_Qt_5_7_1_for_iOS-Release/install/lib/libcommon.a (2 slices)
Undefined symbols for architecture i386:
"CalendarEvent::staticMetaObject", referenced from:
qt_meta_extradata_QMLEventWrapper in moc_qmleventwrapper.o
int qRegisterMetaType(char const*, CalendarEvent::APM_TYPE*, QtPrivate::MetaTypeDefinedHelper::Defined) && (!(QMetaTypeId2::IsBuiltIn))>::DefinedType) in moc_qmleventwrapper.o
"CalendarEvent::colorHex() const", referenced from:
QMLEventWrapper::color() const in qmleventwrapper.o
...
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
...
** BUILD FAILED **
The following build commands failed:
Ld myproject.build/Release-iphonesimulator/myproject.build/Objects-normal/i386/myproject normal i386
(1 failure)
make[1]: * [xcodebuild-release-iphonesimulator] Error 65
make: * [sub-app-make_first] Error 2
Thanks in advance
You are trying to run it on an simulator, which supports architectures i386/x86_64 (the processor of your computer).
Workaround:
Test on a device instead of the simulator
Solution:
Build all used libraries for i386/x86_64 also.
Turned out it was a bug in qmake.
In a very desperate attempt I installed Qt 5.8.0 and tried compiling again. Worked like a charm.
I was using Qt 5.7.1 before which tried to build the libraries for the iPhone instead of the simulator resulting in the wrong architecture.
See https://bugreports.qt.io/browse/QTBUG-58007
I'm trying to deploy a C++ QT application currently for Mac, and will move onto Windows soon, but I'm currently having problems.
I've downloaded the QT source to compile statically. I first cd to the downloaded source directory, then run ./configure -static, which runs fine, but does say WARNING: Unable to find file .device.vars, not sure if that's a problem or not. Then I try to run make sub-src, but it says No rule to make target 'sub-src'. Stop., so I try plain old make instead, which runs for a while, then gives me the error:
Undefined symbols for architecture x86_64:
"std::bad_alloc::bad_alloc()", referenced from:
qBadAlloc() in libQt5Core_debug.a(qglobal.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]: *** [qmleasing] Error 1
make[2]: *** [sub-qmleasing-make_first] Error 2
make[1]: *** [sub-tools-make_first] Error 2
make: *** [module-qtdeclarative-make_first] Error 2
Googling yields no results (from what I can see) :( How can I successfully statically build QT so I can deploy my application, or if statically building is no longer an option, how else can I deploy my app?
Thanks for any help in advance!
EDIT: Probably should add that I'm running OSX 10.8.3, 64 bit, QT dynamically installed under /Applications/QT, attempting to build this in a folder on my desktop.
I have written my pass in llvm/lib/Transforms, and its called createABCDPass. I have added the following code in my pass:
namespace llvm { FunctionPass *createABCDPass(); }
FunctionPass *llvm::createABCDPass() { return new AbcRemoval(); }
where AbcRemoval is the class of the pass.
After that, I have done a forward declaration in lib/CodeGen/LLVMTargetMachine.cpp in order to recognize my pass:
namespace llvm { FunctionPass *createABCDPass(); }
PM.add(createABCDPass());
However, when I run make on llvm, i get the following error:
llvm[2]: Linking Release executable llc (without symbols)
Undefined symbols:
"llvm::createABCDPass()", referenced from:
llvm::LLVMTargetMachine::addCommonCodeGenPasses(llvm::PassManagerBase&, llvm::CodeGenOpt::Level, bool, llvm::MCContext*&)in libLLVMCodeGen.a(LLVMTargetMachine.o)
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [/Users/.../llvm/Release/bin/llc] Error 1
make[1]: *** [llc/.makeall] Error 2
make: *** [all] Error 1
Does anybody know why am I getting this error? Thanks!
Ah, I fixed it in the end by renaming the pass module to -libLLVM_xxx. Apparently you got to name it libLLVM_"something" in order for it to run with all the other passes in LLVM dynamically. Not sure why, but it works!
You have to link your pass to llc. By default llc pulls almost nothing from lib/Transforms, so your pass won't be linked in to llc.