I'm learning how to use cmake to build a project that is using a dynamic library, but I have encountered such error. (Operating System: macOS Big Sur 11.5.2)
According to the documentation, it can be ran by this command:
clang++ ./src/write.cpp -o ./src/write -I./include -L./lib -lxl
But I just can't do it with cmake.
[ 33%] Linking CXX shared library liblibxl.dylib
[ 33%] Built target libxl
[ 66%] Building CXX object CMakeFiles/write.dir/src/write.cpp.o
[100%] Linking CXX executable write
Undefined symbols for architecture x86_64:
"_xlCreateBookA", referenced from:
_main in write.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]: *** [write] Error 1
make[2]: *** [CMakeFiles/write.dir/all] Error 2
make[1]: *** [CMakeFiles/write.dir/rule] Error 2
make: *** [write] Error 2
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(cmaketest VERSION 1.0)
set(SRC_DIR ${PROJECT_SOURCE_DIR}/src)
set(LIB_DIR ${PROJECT_SOURCE_DIR}/lib)
include_directories(${PROJECT_SOURCE_DIR}/include)
add_executable(write ${SRC_DIR}/write.cpp)
add_library(libxl SHARED ${LIB_DIR}/libxl.dylib)
set_target_properties(libxl PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(write libxl)
write.cpp
#include <iostream>
#include "libxl.h"
using namespace libxl;
int main()
{
Book* book = xlCreateBook();
if(book)
{
Sheet* sheet = book->addSheet("Sheet1");
if(sheet)
{
sheet->writeStr(2, 1, "Hello, World !");
sheet->writeNum(3, 1, 1000);
Format* dateFormat = book->addFormat();
dateFormat->setNumFormat(NUMFORMAT_DATE);
sheet->writeNum(4, 1, book->datePack(2008, 4, 22), dateFormat);
sheet->setCol(1, 1, 12);
}
if(book->save("example.xls")) {
std::cout << "File example.xls has been created." << std::endl;
}
book->release();
}
return 0;
}