Error : LNK1104 cannot open file 'pthread.lib' - c++

I am trying to compile a native Linux C++ application in Windows using Visual Studio 2017. The app uses WebRtc's Acoustic Echo Cancellation(AEC) APIs to negate echo on wav files. Following is the CmakeLists.txt file:
cmake_minimum_required(VERSION 2.8)
project(wav-aec)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
add_subdirectory(gflags)
add_definitions("-DWEBRTC_NS_FLOAT -DWEBRTC_WIN -DNOMINMAX")
#-DWEBRTC_UNTRUSTED_DELAY -DWEBRTC_LINUX -DWEBRTC_POSIX
include_directories(
webrtc
webrtc/webrtc/common_audio/signal_processing/include
webrtc/webrtc/modules/audio_coding/codecs/isac/main/include
)
set(WEBRTC_SRC_
base/buffer.cc
base/checks.cc
...
...
#system_wrappers/source/rw_lock_posix.cc
system_wrappers/source/trace_impl.cc
#system_wrappers/source/trace_posix.cc
)
function(prepend_path var prefix)
set(listVar "")
foreach(f ${ARGN})
list(APPEND listVar "${prefix}/${f}")
endforeach(f)
set(${var} "${listVar}" PARENT_SCOPE)
endfunction(prepend_path)
prepend_path(WEBRTC_SRC webrtc/webrtc ${WEBRTC_SRC_})
add_executable(webrtc-audioproc webrtc-audioproc.cpp ${WEBRTC_SRC})
target_link_libraries(webrtc-audioproc gflags pthread)
When I try to build it, I get the following errror:
Error : LNK1104 cannot open file 'pthread.lib'
Here is the link to the only linux dependent source file(cpp) of the project:
https://github.com/lschilli/wav-aec/blob/master/webrtc-audioproc.cpp
What will be the right approach to port the code from Linux to windows?
Whats is Windows equivalent of gflags and pthread? And what necessary changes needs to go to CmakeLists.txt?
P.S: I have already added pthread header, dll and libs to Visual Studio directory manually.

If 'missing pthread library' is the only error, you can use pthread-w32. We have successfully used it in some of our cross-platform apps requiring pthread.
They have libraries for both 64-bit and 32-bit. You can download and add it into your project. You haven't mentioned your toolset - their libraries are named differently depending on your toolset (MSVC or GNU) so you need to pick the right one. Check out their FAQ.
Hope it helps.

You need to us the actual lib file which is typically not "pthread.lib". It's most likely "pthreadVC3.lib" or "pthreadVC2.lib". Find the actual name by looking in the lib directory of your source package. You might see other lib files in there like "pthreadVCE3.lib" and "pthreadVSE3.lib", but you want to link "pthreadVC3.lib".
You can either add this in the project settings, or add the following code:
#pragma comment(lib,"pthreadVC3.lib")
To add it to the project settings:
Go to project properties->Configuration Properties->Linker->General and add your library path to Additional Library Directories
Go to project properties->Configuration Properties->Linker->Input and add the lib file (such as "pthreadVC3.lib") to Additional Dependencies
Make sure you have the correct version of pthread to match your compile settings, ie x86/x64.
In my case, I am using VCPkg for package management and I installed pthreads using the following commands:
vcpkg install pthread:x86-windows
vcpkg install pthread:x64-windows
And my package lib directory is "C:\vcpkg\installed\x64-windows\lib"
I additionally had to add the following to my project settings as vcpkg wasn't integrating automatically:
Go to project properties->Configuration Properties->VC++ Directories and add "C:\vcpkg\installed\x64-windows\include" to Include Directories
Go to project properties->Configuration Properties->VC++ Directories and add "C:\vcpkg\installed\x64-windows\lib" to Library Directories

Related

Migrating a Visual Studio C++ Project to Linux and CMake

I'm currently trying to move from Windows 10 to Linux (Pop!_OS), but I'm having trouble getting my C++ Project to compile and run correctly on the latter. My C++ project was created using Visual Studio, where I also specified the include folders, library folders, what should be linked, etc in the solution properties. I now want to switch to writing my code using Neovim and not Visual Studio (or Visual Studio Code) and have tried compiling it via G++. I quickly noticed that my include files weren't recognized, so I tried to use CMake and created a CMakeLists.txt. I tried using both
INCLUDE_DIRECTORIES()
and
TARGET_INCLUDE_DIRECTORIES()
but no matter what path I enter, my included files were not recognized. Even when I used a path to the specific include file that caused the first error, it still wasn't recognized.
My goal would be that I can specify an include folder and a library folder, so that I can just add files and folders in these and that the new files and folders automatically get recognized when compiling (i.e I would not have to edit the CMakeLists.txt in the future). Is that even possible with CMake and if yes, does anyone know where i can find further information about that or does anyone have a CMakeLists.txt file that does this? If no, would I have to specify each and every file and folder in the CMakeLists.txt file and do the same for every new include and library?
Project structure:
Overall folder
\- build
\- include
---> includeFolder1
---> includeFolder2
---> ...
\- libs
---> library1.lib
---> library2.lib
---> ...
\- src
--> main.cpp
--> other .cpp's and .h's
--> other folders with .cpp's and .h's
I've tried compiling with G++ and CMake, but both did not work, no matter what I specified as the include and library paths.
I have found the problem that caused my errors. The problem wasn't with CMake, it was with Windows and Linux specific details. I always received errors like "<foo\foo.h> no such file or directory", which led me to think that CMake couldn't find the include directory or the files in it. The problem, however, is with the include path itself. On Windows, paths can be given with a backslash ('\') but on Linux, paths are denominated with a forward slash ('/'). So in my example, the path to the file was "../foo/foo.h" but my code had "#include <foo\foo.h>". So when migrating a project from Windows to Linux, be sure to watch out for backslashes in your #include statements!
Below is a template CMakeLists.txt, that should be a good starting point if you want to migrate your Visual Studio project to Linux. I've used glfw (+ glad) as an example library:
cmake_minimum_required(VERSION 3.20)
project(ExampleProject)
add_executable(${PROJECT_NAME} src/glad.c src/main.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE include)
target_link_libraries(${PROJECT_NAME} GL dl glfw)

Visual Studio's CMake with vcpkg: Error gdal is not found

I have been trying to build a cmake c++ project. More specifically I am trying to use the gdal library in this project. In the CMakeLists.txt it says find_library(GDAL gdal) after doing some research i found, that visual studio can open cmake files by default as mention in this thread: https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=vs-2019.
Moreover, visual studio should also automatically include the gdal library once i have set it up with vcpkg correctly. I've already downloaded the x64-windows version of the library (vcpkg install gdal:x64-windows) in order to build for the right architecture and made it available via vcpkg integrate install on a user-wide scope.
After some trial and error, everything works fine now, the toolchain gets included accordingly and the library is found automatically, resulting in a configuration like that:
However, when trying to include the header files (or anything else; see code snippet), visual studio does not seem to link the library correctly as it will result in the error message: cannot open source file "gdal/gdal.h".
#include <gdal/ogrsf_frmts.h>
#include <gdal/gdal.h>
#include <gdal>
Where should I further investigate?
As others have said vcpkg integrate install and vcpkg.cmake don't work together the reason being:
set_target_properties(${name} PROPERTIES VS_USER_PROPS do_not_import_user.props)
set_target_properties(${name} PROPERTIES VS_GLOBAL_VcpkgEnabled false)
this deactivates the integration. The reason to deactivate the integration is so that you don't write an incomplete CMakeLists.txt (e.g. missing the include directory or not linking all required libraries).
As such replace find_library(GDAL gdal) with find_package(GDAL REQUIRED) and target_link_libraries against the target GDAL::GDAL (https://cmake.org/cmake/help/v3.17/module/FindGDAL.html)

How to link a library that was created from add_subdirectory in CMake?

I am trying to build a program using CMake that depends on a third party library. This third party library contains a CMakeLists.txt file so what I want to do is keep the source code of the third party library within my project directory, and build it using add_subdirectory(path/to/lib), and then link my target against the static library that the third party library generated.
my CMakeLists.txt:
cmake_minimum_version(VERSION 3.10)
project(my_project)
add_subdirectory("${CMAKE_SOURCE_DIR}/extern/somelib")
# my-code:
# somelib CMakeLists.txt file has a project name: SOMELIB
# which lets me access the directory where the files are built
# on windows it builds to /Release, on mac and linux it just builds
# to the binary dir
set(SOMELIB_LIBS "${SOMELIB_BINARY_DIR}/Release")
add_executable(my_program my_main.cpp)
target_link_libraries(my_program "${SOMELIB_LIBS}/SOMELIB.lib" "${SOMELIB_LIBS}/SOMELIBmain.lib")
I then make a build directory and from that directory I do:
cmake -G "Visual Studio 15 2017" ..
cmake --build .
The build command fails with a "LINK : fatal error LNK1181: cannot open input file 'extern/somelib/Release/SOMELIBmain.lib' ..."
My workaround for now has been to comment out the part that says "#my-code", build the somelib dependency first which generates the static libraries, and then uncomment out my-code and build again which then works correctly.
How can I tell CMake to build the subdirectory first, and then link against the static libraries that it generated?
Short answer: tell CMake that there is dependency between its targets.
target_link_libraries(my_program PRIVATE SOMELIB SOMELIBmain)
CMake will evaluate SOMELIBs locations for you and link my_program against SOMELIB and SOMELIBmain[1]. Works for both Debug and Release configurations and also for Linux.
You shouldn't have to worry where CMake places build files[2] and this is what so called "modern CMake" is trying to achieve. I will leave here just brief description but check link on the bottom of answer[3].
Anyway, the key concept is that whenever you create library/executable (target), you list its usage+build requirements and dependencies to other targets. You declare dependencies using target_link_libraries(<your_lib> KEYWORD <dependencies>). Such line will not only make <you_lib> link against listed dependencies, it will inherit their usage requirements (usually, public include directories) and order CMake to build dependent libraries before <your_lib>.
The beauty of it is that even if SOMELIB does not follow modern CMake ideas (does not declare build/usage requirements), you still should be able to do just with this single line.
[1] I assumed that CMake targets are named same as output library names, but it is not mandatory. For OP's case it turned out that static library targets are suffixed with -static, so he had to write SOMELIB-static SOMELIBmain-static
[2] Unfortunately, it is not so easy with shared libraries on Windows (DLLs)
[3] I'd start with this blog entry: https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/

CLion MinGW freeglut opengl library linking

I am trying to follow this guide to configure my freeglut and opengl .
I am used to the intellij enviroment via android studio so i would like to work in Clion.
I am stuck at the part....
Libraries: the OpenGL library "libopengl32.a", GLU library
"libglu32.a" and GLUT library "libfreeglut.a" are kept in
"\lib" directory. This directory is in the implicit
library-path. Nonetheless, we need to include these libraries in
linking. They shall be referred to as "opengl32", "glu32", "freeglut"
without the prefix "lib" and suffix ".a".
how do i add this in Clion ?
CLion uses cmake for building your projects. Follow below steps to add link libraries to your project.
Open the CMakeLists.txt file in your project and add,
target_link_libraries(<target executable> libopengl32.a libglu32.a libfreeglut.a)
to that file.
target executable is the executable which you want to link these libraries. Normally this is your project name as defined in add_executable.
Note: Cmake will show you an error if you place target_link_libraries before add_executable.

How to use FFTW with cmake on windows?

I am building my project for Visual Studio with CMake. I used this lines in my CMakeList.txt to include FFTW3.
find_library(FFTW_LIBRARY
NAMES fftw3 fftw)
set(FFTW_LIBRARIES "${FFTW_LIBRARY}")
I got this error from CMake:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
FFTW_LIBRARY
linked by target "windows_SHT" in directory C:/...
I think I did not install fftw properly. I got .dll .lib and .h files in a folder but I don't know how to explain to CMake where is the library.
Specify additional paths for find_library to use.
See: http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_library
You will need to include the path to your custom FFTW installation.
A similar technique will need to be used when you use find_file to locate the headers
http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_file