Undefined symbols using WxWidgets and Conan - c++

I am trying to adopt the Conan C++ package manager. I have followed along with the getting started have managed to get it working with most projects. However I am having linking issues with WxWidgets that I can't seem to fix.
linker error:
[ 23%] Linking CXX executable ../bin/wx_sample
Undefined symbols for architecture x86_64:
"Frame::onMenuFileQuit(wxCommandEvent&)", referenced from:
__GLOBAL__sub_I_frame.cpp in frame.cpp.o
"Frame::onMenuFileSave(wxCommandEvent&)", referenced from:
__GLOBAL__sub_I_frame.cpp in frame.cpp.o
"Frame::onMenuFileAbout(wxCommandEvent&)", referenced from:
__GLOBAL__sub_I_frame.cpp in frame.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]: *** [bin/wx_sample] Error 1
make[1]: *** [src/CMakeFiles/wx_sample.dir/all] Error 2
make: *** [all] Error 2
conanfile.txt
[requires]
wxwidgets/3.1.4#bincrafters/stable
[generators]
cmake
CmakeFile.txt - project root -
cmake_minimum_required(VERSION 3.17)
project(wx_sample)
set(CMAKE_CXX_STANDARD 17)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
include_directories(src)
add_subdirectory(src)
CmakeFile.txt -- src folder --
set(BINARY ${CMAKE_PROJECT_NAME})
file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.h *.cpp)
set(SOURCES ${SOURCES})
add_executable(${BINARY} ${SOURCES})
target_link_libraries(${BINARY} ${CONAN_LIBS})
conan default profile:
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=apple-clang
compiler.libcxx=libc++
compiler.version=12.0
os=Macos
os_build=Macos
I am not sure if I need to include deps into my conanfile, though I thought that Conan manages this automatically? If I remove the offending sections listed in the error, the application links successfully.

EDIT:
You were right on the CONAN_LIBS global. Your CMake structure looks fine. You could try to call conan_basic_setup() from the src CMakeLists.txt.
If that doesn't work the issue might be in your conanfile.py.
https://github.com/bincrafters/conan-wxwidgets/commit/37b6669229ec0e68593065a700c360d23a914bac

Related

Unable to run SFML project with CMake on arm64

I am using MacBook with M1 and I try to create a project with SFML.
I downloaded a snapshot of SFML with files built for arm64
I verified it using file command
Example:
libsfml-graphics.dylib: Mach-O 64-bit dynamically linked shared library arm64
I placed these files in ~/Library/Frameworks along with extlibs directory content.
I set a CMake flag: -DCMAKE_OSX_ARCHITECTURES=arm64
But I still am unable to run this project.
Error:
[ 50%] Linking CXX executable myProject
Undefined symbols for architecture arm64:
"__ZN2sf6StringC1EPKcRKSt6locale", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
make[3]: *** [myProject] Error 1
make[2]: *** [CMakeFiles/myProject.dir/all] Error 2
make[1]: *** [CMakeFiles/myProject.dir/rule] Error 2
make: *** [myProject] Error 2
This is my whole CMakeLists.txt file:
cmake_minimum_required(VERSION 3.19)
project(myProject)
add_executable(myProject main.cpp)
set(CMAKE_CXX_COMPILER "/opt/homebrew/bin/g++-11" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCMAKE_OSX_ARCHITECTURES=arm64")
include_directories(/usr/local/include)
find_package(SFML 2.5 COMPONENTS system window graphics network audio REQUIRED)
include_directories(${SFML_INCLUDE_DIRS})
target_link_libraries(myProject sfml-system sfml-window sfml-graphics sfml-audio sfml-network)
I also tried to use dylib instead of framework, but it was unsuccessful also. I'd be grateful for any help, I can't figure out what I did wrong.

MacOs - Compiling c++ OpenCv returns symbol(s) not found for architecture x86_64

I want to try this OpenCv code on MacOs.
I have followed this tutorial to install OpenCv on MacOs. (Before I tried to install with homebrew)
I have the following CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
SET(OpenCV_DIR <path>/installation/OpenCV-master/lib/cmake/opencv4)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " config: ${OpenCV_DIR}")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
# Declare the executable target built from your sources
#add_executable(opencv_example example.cpp)
project(intro_PCA)
add_executable(myapp introduction_to_pca.cpp)
# Link your application with OpenCV libraries
#target_link_libraries(opencv_example ${OpenCV_LIBS})
include_directories(
<path>/installation/OpenCV-master/include/opencv4
)
install(TARGETS myapp DESTINATION ../0-BRIQUE_PCA/briquepca/)
From the folder build I compile the code with the following commands:
$ cmake ..
$ cmake --build . --config Release
The compilation ends with the following error:
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]: *** [myapp] Error 1
make[1]: *** [CMakeFiles/myapp.dir/all] Error 2
make: *** [all] Error 2
I found here that one should add following stuff:
-libopencv_core \
-libopencv_imgproc \
-libopencv_features2d \
-libopencv_highgui
But I do not understand where I have to add these lines in my CMakeLists.txt
Does someone have an idea?
You can link libraries with the command: target_link_libraries(myapp ${OpenCV_LIBS}), it will link myapp to all OpenCV libraries defined by find_package(OpenCV REQUIRED).

Undefined symbols when building libuv

I need to use libuv with my library. Since I cannot link it two static libraries I decided to include the source for libuv along with my code. I have a .cmake file that downloads libuv, checks out the right tag and adds the source files to a variable:
include(DownloadProject.cmake)
download_project(PROJ libuv
GIT_REPOSITORY https://github.com/libuv/libuv.git
GIT_TAG v1.10.0
${UPDATE_DISCONNECTED_IF_AVAILABLE}
)
exec_program(COMMAND "./autogen.sh" WORKING_DIRECTORY ${libuv_SOURCE_DIR})
exec_program(COMMAND "./configure" WORKING_DIRECTORY ${libuv_SOURCE_DIR})
include_directories(${LIBUVDIR}/include ${LIBUVDIR}/src)
set(LIBUV_SOURCES
${LIBUVDIR}/include/uv.h
${LIBUVDIR}/include/tree.h
${LIBUVDIR}/include/uv-errno.h
${LIBUVDIR}/include/uv-threadpool.h
${LIBUVDIR}/include/uv-version.h
${LIBUVDIR}/src/fs-poll.c
${LIBUVDIR}/src/heap-inl.h
${LIBUVDIR}/src/inet.c
${LIBUVDIR}/src/queue.h
${LIBUVDIR}/src/threadpool.c
${LIBUVDIR}/src/uv-common.c
${LIBUVDIR}/src/uv-common.h
${LIBUVDIR}/src/version.c
)
etc.
I then add add the list of libuv source files to the list of source files for my project and link the library with its dependencies:
include(libuv.cmake)
# Build library
set(SOURCE_FILES
<my sources>
${LIBUV_SOURCES})
add_library(databaseclient STATIC ${SOURCE_FILES})
set(CMAKE_THREAD_PREFER_PTHREAD 1)
set(THREADS_PREFER_PTHREAD_FLAG 1)
include(FindThreads)
target_link_libraries(databaseclient PUBLIC Threads::Threads)
But when I run make I get the following error:
Undefined symbols for architecture x86_64:
"_pthread_barrier_destroy", referenced from:
_uv_barrier_destroy in libdatabaseclient.a(thread.c.o)
"_pthread_barrier_init", referenced from:
_uv_barrier_init in libdatabaseclient.a(thread.c.o)
"_pthread_barrier_wait", referenced from:
_uv_barrier_wait in libdatabaseclient.a(thread.c.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]: *** [tests/unit_tests] Error 1
make[1]: *** [tests/CMakeFiles/unit_tests.dir/all] Error 2
make: *** [all] Error 2
unit_tests is my unit tests executable.
I think there's something I'm not linking against, just don't know what. Any clues?
Hi I just ran into this same problem, it seems pthread_barrier primitives are not implemented on MacOS despite it's claims of POSIX compliance. LibUV implements it's own versions using other threading primitives. If you add ${LIBUVDIR}/src/unix/pthread-barrier.c to your list of libuv c files it should link correctly.
Here is what I have in CMakeLists.txt for libuv
set(LIBUVDIR libuv)
include_directories(${LIBUVDIR}/include ${LIBUVDIR}/src)
set(SOURCES
${LIBUVDIR}/src/fs-poll.c
${LIBUVDIR}/src/inet.c
${LIBUVDIR}/src/threadpool.c
${LIBUVDIR}/src/uv-common.c
${LIBUVDIR}/src/version.c)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
add_definitions(-D_GNU_SOURCE)
set(SOURCES ${SOURCES}
${LIBUVDIR}/src/unix/linux-syscalls.c
${LIBUVDIR}/src/unix/linux-core.c
${LIBUVDIR}/src/unix/linux-inotify.c)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
add_definitions(-D_DARWIN_USE_64_BIT_INODE=1 -D_DARWIN_UNLIMITED_SELECT=1)
set(SOURCES ${SOURCES}
${LIBUVDIR}/src/unix/darwin.c
${LIBUVDIR}/src/unix/darwin-proctitle.c
${LIBUVDIR}/src/unix/fsevents.c
${LIBUVDIR}/src/unix/kqueue.c
${LIBUVDIR}/src/unix/pthread-barrier.c
${LIBUVDIR}/src/unix/proctitle.c)
endif()
include_directories(${LIBUVDIR}/src/unix)
set(SOURCES ${SOURCES}
${LIBUVDIR}/src/unix/async.c
${LIBUVDIR}/src/unix/core.c
${LIBUVDIR}/src/unix/dl.c
${LIBUVDIR}/src/unix/fs.c
${LIBUVDIR}/src/unix/getaddrinfo.c
${LIBUVDIR}/src/unix/getnameinfo.c
${LIBUVDIR}/src/unix/loop-watcher.c
${LIBUVDIR}/src/unix/loop.c
${LIBUVDIR}/src/unix/pipe.c
${LIBUVDIR}/src/unix/poll.c
${LIBUVDIR}/src/unix/process.c
${LIBUVDIR}/src/unix/signal.c
${LIBUVDIR}/src/unix/stream.c
${LIBUVDIR}/src/unix/tcp.c
${LIBUVDIR}/src/unix/thread.c
${LIBUVDIR}/src/unix/timer.c
${LIBUVDIR}/src/unix/tty.c
${LIBUVDIR}/src/unix/udp.c)
add_library(uv STATIC ${SOURCES})
target_link_libraries(uv pthread)

cmake simple sub-directory not including files

I have read and tried just about every tutorial/wiki/SO post, page, snippet I could find to get this CMAKE working....
I have a super simple directory structure:
ROOT/
|- CMakeLists.txt
|- main.cpp
|- sub/
|-CMakeLists.txt
|-subx/
|-CMakeLists.txt
|-subx.h
|-subx.cpp
|-suby/
|-CMakeLists.txt
|-suby.h
|-suby.cpp
The main.cpp is a super simple cpp program:
//omitting all unnecessary code
int main() {
subx s;
s.defined_method();
s.another_defined_method(1);
return 0;
}
You can assume, for everyone's sake that the subx and suby definitions are correct and work just fine, because they do when I compile by hand.
When I compile by CMake I get the following error:
"/path/to/cmake" --build /path/to/Debug --target CS220_Project -- -j 4
Linking CXX executable simple_project
Undefined symbols for architecture x86_64:
"subx::defined_method()", referenced from:
_main in main.cpp.o
"subx::another_defined_method(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]: *** [simple_project] Error 1
make[2]: *** [CMakeFiles/simple_project.dir/all] Error 2
make[1]: *** [CMakeFiles/simple_project.dir/rule] Error 2
make: *** [simple_project] Error 2
The root CMakeLists.txt file looks:
cmake_minimum_required(VERSION 2.8.4)
project(simple_project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_subdirectory(sub)
add_executable(simple_project ${SOURCE_FILES})
Sub CMakeLists.txt file looks:
add_subdirectory(subx)
add_subdirectory(suby)
subx & suby CMakeLists.txt file looks: (they include their respective distinction)
set(SUBX_SOURCES subx.cpp)
#Add any files in this directory
add_executable(SUBX ${SUBX_SOURCES})
I've tried things like add_library, file (glob), etc. I cannot, for the life of me get files that are in any sub-directory to compile with the main.cpp program.
Depends on what exactly you want the subprojects to be. The way I understand it, subx and suby are libraries, which should be linked to the main executable:
ROOT/CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
project(simple_project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_subdirectory(sub)
add_executable(simple_project ${SOURCE_FILES})
target_link_libraries(simple_project SUBX SUBY)
ROOT/subx/CMakeLists.txt
set(SUBX_SOURCES subx.cpp)
#Add any files in this directory
add_library(SUBX ${SUBX_SOURCES})
(dtto for suby)

Cmake compile with Frameworks on Mac OSX and treat .cpp files like .m/.mm

I'm looking for a tip to get the following to work, here is my CMakeLists.txt
# cmake_minimum_required(2.8.2)
project(boilerplate)
# base files
set(src_files
src/greet.h
src/main.cpp
)
# if on OSX, these files are needed
if(APPLE)
SET(CMAKE_EXE_LINKER_FLAGS "-framework Foundation -w")
set(src_files
${src_files}
src/mac/greet.mm
src/mac/greeting.h
src/mac/greeting.m
)
endif()
# if on windows, these files are needed
if(WIN32)
set(src_files
${src_files}
src/win/greet.cpp
)
endif()
add_executable(greeting
${src_files}
)
I require that on OSX the .cpp files are treated like .mm files (but on Windows, not) and that I can load the core foundation, etc frameworks... I'm a complete cmake newbie, so I can't even begin to know where to start, but I hope I'm somehow in the right direction, current output is:
$ cmake CMakeLists.txt && make
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/leehambley/Projects/watched.it-client
Scanning dependencies of target greeting
[ 33%] Building CXX object CMakeFiles/greeting.dir/src/mac/greet.o
Linking CXX executable greeting
Undefined symbols:
"greet()", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [greeting] Error 1
make[1]: *** [CMakeFiles/greeting.dir/all] Error 2
make: *** [all] Error 2
1
This turned out to be rather easy once I understood what was supposed to be happening under the hood:
set(CMAKE_CXX_FLAGS "-x objective-c++")
Which tells gcc that you want to set the language property (-x language, in man gcc) to Objective-C++.
You can also do this for individual files with:
set_source_files_properties(${SOURCE_FILES} PROPERTIES
COMPILE_FLAGS "-x objective-c++")
I've had mixed success with both, probably highlighting some of the things I don't fully understand about CMake.