How does Auto-Linking libraries in C++ work? - visual-c++

I am trying to auto-link following libraries. I've never used auto-linking feature and not sure how it works exactly. From my research this should work. When I include the dll to the project besides the SDL libraries I get following errors:
https://i.gyazo.com/e49a636ddad428fa48acdee78c930293.png
What are the steps to get Auto-linking working. Don't I need to specify some kind of path for this? Does it have to be in specific order?
This code is inside the DLL:
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_ttf.h>
#pragma comment(lib, "SDL2.lib")
#pragma comment(lib, "SDL2main.lib")
#pragma comment(lib, "SDL2_image.lib")
#pragma comment(lib, "SDL2_mixer.lib")
#pragma comment(lib, "SDL2_ttf.lib")
Thank you for taking your time to answer.

You haven't explicitly specified what platform or development environment you're using, but it looks like you're using a recent version of Visual Studio. You need to configure the "VC++ Directories" settings in your project to include the paths for the lib files, under "Library Directories". These pragma directives will instruct the linker to search for the lib files in these directories.

The compiler complains that it can't find the header sdl.h, it hasn't gotten to the linking part (and I'm guessing it would have trouble there too).
To solve:
Add the path to the SDL headers here: project properties\ C/C++ \ General \Additional include directories
Add the path to your import libs here: project properties \ Linker \ General \ Additional Library directories.
Don't fiddle with 'VC++ directories' - as the name suggests, it is where the toolchain seeks various VC components (crt, mfc, open mp, windows sdk etc.). Messing this up could result in extremely hard to diagnose build failures.

Related

Error Compiling C++/Cuda extension with Pytorch Cuda c++ in MSVC using CMake

I am trying to build a c++/cuda extension with Pytorch following the tutorial here, (with instructions how to use pytorch with c++ here). My environment details are:
Using Microsoft Visual Studio 2019 version 16.6.5
Windows 10
libtorch c++ debug 1.70 with cuda 11.0 installed from the pytorch website
I am using this cmake code where I set the include directory for python 3.6 and the library for python36.lib
cmake_minimum_required (VERSION 3.8)
project ("DAConvolution")
find_package(Torch REQUIRED)
# Add source to this project's executable.
add_executable (DAConvolution "DAConvolution.cpp" "DAConvolution.h")
include_directories("C:/Users/James/Anaconda3/envs/masters/include")
target_link_libraries(DAConvolution "${TORCH_LIBRARIES}" "C:/Users/James/Anaconda3/envs/masters/libs/python36.lib")
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET DAConvolution
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:DAConvolution>)
endif (MSVC)
I set the CMake command arguments to be -DCMAKE_PREFIX_PATH=C:\libtorch (my path to libtorch debug mentioned above). I am building with the x64-debug option in MSVC version (as building with the x-64 Release option gives me a torch-NOTFOUND error).
The example DAConvolution.cpp file is:
#ifdef _DEBUG
#undef _DEBUG
#include <python.h>
#define _DEBUG
#else
#include <python.h>
#endif
#include <torch/extension.h>
Where I have undefined the _DEBUG flag so that the linker does not look for the python36_d.lib file (which I do not have).
I am getting a linking error:
Simply including torch.h works fine, but when I want to include the extension header thats when I get these problems, as it uses Pybind 11 I believe. Any insights much appreciated. I have tried to include all the info I can, but would be happy to give more information.
For Windows and with Visual studio, you are better to work with the Visual Studio rather than the CMake.
Just create a simple Console Application, go to the project's Properties, change the Configuration type to Dynamic Library (dll), Configure the include and Library directories, add the required enteries to your linker in Linker>Input (such as torch.lib, torch_cpu.lib, etc) and you are good to go click build, and if you have done everything correctly you'll get yourself a dll that you can use (e.g loading it using torch.classes.load_library from Python and use it.
The Python debug version is not shipped with Anaconda/ normal python distribution, but if you install the Microsoft Python distribution which I believe can be downloaded/installed from Visual Studio installer, its available.
Also starting from Python 3.8 I guess the debug binaries are also shipped.
In case they are not, see this.
For the cmake part you can follow something like the following. This is a butchered version taken from my own cmake that I made for my python extension some time ago.
Read it and change it based on your own requirements it should be straight forward :
# NOTE:‌
# TORCH_LIB_DIRS needs to be set. When calling cmake you can specify them like this:
# cmake -DCMAKE_PREFIX_PATH="somewhere/libtorch/share/cmake" -DTORCH_LIB_DIRS="/somewhere/lib" ..
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(DAConvolution)
find_package(Torch REQUIRED)
# we are using the C++17, if you are not change this or remove it altogether
set(CMAKE_CXX_STANDARD 17)
#define where your headers and libs are, specify for example where your DaConvolution.h resides!
include_directories( somewhere/Yourinclude_dir ${TORCH_INCLUDE_DIRS})
set(DAConvolution_SRC ./DAConvolution.cpp )
LINK_DIRECTORIES(${TORCH_LIB_DIRS})
add_library(
DAConvolution
SHARED
${DAConvolution_SRC}
)
# if you use some custom libs, you previously built, specify its location here
# target_link_directories(DAConvolution PRIVATE somewhere/your_previously_built_stuff/libs)
target_link_libraries(DAConvolution ${TORCH_LIB_DIRS}/libc10.so)
target_link_libraries(DAConvolution ${TORCH_LIB_DIRS}/libtorch_cpu.so)
install(TARGETS DAConvolution LIBRARY DESTINATION lib )
Side note:
I made the cmake for Linux only, so under Windows, I always use Visual Studio (2019 to be exact), in the same way I explained earlier. its by far the best /easiest approach imho. Suit yourself and choose either of them that best fits your problem.

c++ Android NDK and include issues

I am getting into building c++ modules for my apps. I cannot get boost or system to work.
I do the following:
#include <boost>
or
#include <system>
and BOTH things result in "boost no such file or directory" and "system no such file or directory.
I cannot begin to list the things I have attempted because the list is way too long. I have followed multiple links leading me to lots of things. But nothing seems to work. If I can get #include <system> to work then I won't need boost. I would prefer to get #include <system> working for that will allow me to execute command line commands inside my module.
Thanks everyone!
I hope you include stlport_static to the project. I think "APP_STL:=stlport_static" must be in Application.mk file.
Did you see these links:
http://stoflru.org/questions/23783680/android-ndk-include-boost-library
Include Boost C++ library in android
Android NDK: Including boost c++ library

CMAKE auto header file dependency

Question is similar to this question
Handling header files dependencies with cmake
I have sample program dir having main.c main.h and CMakeLists.txt
main.h contents are
#ifndef MAIN_H
#define MAIN_H
int t=3;
int y=2;
#endif
main.c contents are
#include <main.h>
#include<stdio.h>
int main(){
printf("%d apple",t);
}
and CMakeLists.txt
PROJECT( test )
AUX_SOURCE_DIRECTORY(. test_SRCS)
include_directories(.)
ADD_EXECUTABLE (main ${test_SRCS})
but cmake is not rebuilding main.c on modification of header file.
I want it to auto-generate header file dependency.
Is it possible using cmake ?
if not is there any other tool which can do that ?
As mentioned in my comment, I have tried out your example and things were working fine: if main.h was modified then main.c would be recompiled.
My installation of CMake (version 2.8.0) told me to add
cmake_minimum_required(VERSION 2.8)
to the CMakeLists.txt file, but that is all of the adjustments I needed.
Answering this for others that google search...
I ran into this problem with one of my projects. As it turns out I added the header to the cpp file after running cmake. Re-running cmake fixed the problem. If you run into this, try that and see if it fixes the issue.
From the cmake 2.8.0 documentation of AUX_SOURCE_DIRECTORY:
It is tempting to use this command to avoid writing the list of source
files for a library or executable target. While this seems to work,
there is no way for CMake to generate a build system that knows when a
new source file has been added. Normally the generated build system
knows when it needs to rerun CMake because the CMakeLists.txt file is
modified to add a new source. When the source is just added to the
directory without modifying this file, one would have to manually
rerun CMake to generate a build system incorporating the new file.
Why do you want to avoid creating a list of files? Such lists generally do not change frequently.

static build glew & glfw on linux

In my project directory, i have:
./external/glew, which has glew compiled from source (ran make)
./external/glfw, which has glfw also compiled from source (ran make x11)
in my .c source code:
#include <stdio.h>
#include <stdlib.h>
#include "external/glew/include/GL/glew.h"
#include "external/glfw/include/GL/glfw.h"
i tried to compile using GCC:
gcc test1.c -o test1 -DGLEW_STATIC -L./external/glew/lib -lGLEW -lGLU -lGL \
-L./external/glfw/lib/x11 -lglfw
./external/glew/lib is where the libGLEW.a is and ./external/glfw/lib/x11 is where the libglfw.a is.
and it compiles without error. but then i try to run ./test1 it gives me:
./test1: error while loading shared libraries: libGLEW.so.1.6: cannot
open shared object file: No such file or directory
how do i compile glew & glfw statically?
EDIT 1 Thanks for the help guys. After some help from people in stackoverflow and old nabble I manage to write it down what needs to be done to statically linked GLFW and GLEW and put it on http://www.phacks.net/static-compile-glfw-and-glew/
Static libraries are not linked with -l… but just added to the linker source files. However please double check you really want to link those statically. The problem you have here is that the dynamic linker on *nix systems will by default only look into the system library directories and the path specified in the LD_LIBARY_PATH environment variable.
However it is possible to add relative linker paths to the executable, where libraries are located, too (--rpath linker option). That way you can ship the libraries in a directory relative to your executable, independently from the system libraries. If you do this, you also should look into binreloc

difficulties with "cl.exe" (command line compiler from VisualStudio) and header files!

I have installed Microsoft Visual C++ Express Edition, Version 9.0.30729.1 SP.
The command line compiler that comes with it is at Version 15.00.30729.01 for 80x86.
I have installed OpenCV 20.0a.
I want to compile the following program:
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *img = cvLoadImage("funny-pictures-cat-goes-pew.jpg");
cvNamedWindow("Image:",1);
cvShowImage("Image:",img);
cvWaitKey();
cvDestroyWindow("Image:");
cvReleaseImage(&img);
return 0;
}
The thing is, I do NOT want to use the "visual" aspect of Visual C++, I want to use the command line compiler which is "cl.exe".
When I try to compile this program, I get the error:
C:\visualcpp>cl OpenCV_Helloworld.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.
OpenCV_Helloworld.cpp
OpenCV_Helloworld.cpp(6) : fatal error C1083: Cannot open include file: 'cv.h': No such file or directory
So I tried to specifiy with /I like this
C:\visualcpp>cl /I "C:\OpenCV2.0\src\cv" OpenCV_Helloworld.cpp
And variations thereof, in the hope that /I would somehow tell cl.exe where cv.h is, but I get the same error.
As a side note, I don't know if that's related but I noticed that there is no file "cv.h" in "C:\OpenCV2.0\src\", but rather a file called "_cv.h"! So I changed the header accordingly, but still.
I think I'm able to program in C++ somewhat but I don't understand how to specify header / linker file locations, especially with cl.exe as I have only used gcc before and I don't think I know what I'm doing right now. Please help me to compile this! I want to get started in OpenCV.
First of all, be sure to set up the environment by calling one of the batch files that come with Visual Studio, i.e. vsvars32.bat found in your Visual Studio folder under Common7\Tools. During installation, there is usually also a start menu entry created which opens a console and executes the setup script. This will make sure environment variables are set up properly and the compiler and linker has access to header files, libraries, tools are on your path etc.
You will find the cl command line options listed here for documentation: Compiler Command-Line Syntax
As for OpenCV: Take a look at the directory structure of OpenCV. It's
OpenCVRootFolder\include\opencv\cv.h
so you would usually go ahead and say:
cl /I"X:\OpenCVRootFolder\include" [...] source.cpp /LINK [...]
and in your code, include the cv header via:
#include <opencv\cv.h>
...or you could just go ahead and do
cl /I"X:\OpenCVRootFolder\include\opencv" [...] source.cpp /LINK [...]
and simple include
#include <cv.h>
I don't see why you wouldn't want to use Visual Studio, though. It's just an IDE, there are no features forced upon you or included if you don't want them to.
There is no visual aspect of Visual Studio. It's just a name. All C++ programs are compiled with cl.exe. The C++ compiler has no knowledge of any visual anything - it's just a brand. cl.exe can only be invoked from within Visual Studio, however. It is not a command line compiler.

Resources