How to build a library with cgo using visual studio compiler? - c++

It is a bit of a newbie question, but I can't seems to make it work.
I know I should use CC=, CXX= and i know the vs compiler name is cl though I'm not sure it is also true for the c++ compiler.
Now assuming the compiler name is cl for both I get the error:
go build CC=cl cxx=cl --buildmode=c-shared -o ../lib/core.dll
I get: can't load package: package CC=cl: cannot find package "CC=cl" in any of: ...
If on the other hand I try to compile without the CC / CXX options then it is all working good.
SO to sum it up:
1. what is the c and c++ compiler name for vs?
2. why i get an error when trying to choose which compiler it will use?

I tried to use cgo in visual studio with Liron's method with the latest go 1.13. It's working in a little different way. I cannot use the extracted stub lib to connect the dll to visual studio. Instead I have do something that I do not quite understand to make it working:
Create a .dll (with go 1.13 and gcc compiler) with c-shared build mode.
Create a .lib with c-archive build mode.
Use .lib to compile, and use .dll to access all functions. If I directly use .lib, all functions stall when accessing. If I use the extracted stub lib from dll, the program does not even load successfully.

After a few days of playing around it seems that the only working way to create a library that would work with visual studio is to:
create a .dll (using cgo with go 1.10+)
use the bat file or the method mention in HERE to create a stub lib to connect the dll to visual studio.
a side note it might be also possible to use loadlibrary func (I did not try that as i tried to avoid this method as it seems to be not a recommended approach).

Related

Build path in executables or DLL (Visual Studio C++/Windows) [duplicate]

Per default, when compiling a Visual Studio project in release mode, the complete path to the pdb is put into the image file, e.g.:
c:\myprojects\demo\release\test.pdb
Using an undocumented linker switch (/pdbpath:none) one can force Visual Studio 2008 to reduce the full qualified name of the pdb, e.g:
test.pdb
I need to do the same with a project which is still built using VC6.
I tried the "/pdbpath:none" switch at the project settings level, but the linker complains about this unknown switch.
Does anyone knows a method (or a tool) to accomplish this either when linking a VC6 project or afterwards directly at the image level?
Your best bet is to use pdbstr.exe from MS directly. It allows for direct extraction, update, and misc other functions directly, independent of compiler version (up to the last supported version, which I think is VS2013 right now). We use it to add SVN linkings directly to PDBs which we then store in local symbol stores using srctool.
For newer link.exe versions, the syntax changed.
The option you want is now /pdbaltpath:%_PDB%
It is documented on MSDN: https://msdn.microsoft.com/en-us/library/dd998269.aspx
%_PDB% expands to the file name of the actual .pdb file without any path information
For VC6, you might want to continue using the same compilers but a new version of link.exe.
The Windows Driver Kits also come with a tool named binplace.exe which can modify this information post-build.

How to build Visual Studio Solution under Linux?

I was always using Windows, have very limited Linux experience.
My Visual Studio solution contains 5 C++ projects - 4 of them are static libs, and one is main application (that use these static libs), I want to move it to Ubuntu.
I do not use any windows specific code, so with minor changes I should be abble to compile under Linux.
How to do this? What exactly software should I use under Linux? What should I do with static libs, should I keep using static libs in Linux? How to convert Visual Studio solution to something Linux-like?
upd what if I just download Eclipse in Linux and then file by file, project by project, recreate and copy everything from VC++ to Eclipse? this should work, isn't it? I have just 100-200 files so it's possible to do this by hand.
I can think of two reasonable options. The first one is to create a makefile that will compile everything for you. There was once a utility called Make It So that did this automatically. Their page specifies compatibility with Visual Studio 2010, it might work with Visual Studio 2012 as well.
You can also use cmake. It's a bit more involving to get right, but the end result will be the ability to compile your code more or less anywhere.
Use xbuild? So if you install Mono, then you have xbuild which is the OSS version of msbuild. You can just build your .sln file by something like "xbuild solution.sln"
Clion can generate cmake file automatically for .sln project under linux.
If your code is not dependant on any Window specific library then you can use make the utility to make any lib, bin.
You can also provide different rules to link your library based on your specific requirement. You can also link third party library using Make utility.

Linking to and using libpq with Visual C++ Express 2010

I am trying to access from Visual C++ 2010 Express a Postgres database. I have both on my machine but the SQL calls do not work at all. I have searched many sites. I think that this version of Express does not have any default database connections like the non-express version (especially Visual C++ 2008).
The only thing I could find was the following link and I have following the modifications to the Project Properties area of Visual C++ Express.
http://www.askyb.com/cpp/c-postgresql-example/
When I try to run the C++ code in that website I get errors indicating that it cannot find the functions.
Do you have any ideas of what I am doing wrong? Would it be better for me to use something other than Visual C++ Express 2010 to connect to a postgres db? One of the other Express versions? The regular (non-express) Visual C++?
Thanks
You may also be interested in libpqtypes and libpqxx if you're working with PostgreSQL and libpq. The first provides greatly simplified access to data values from libpq, and the latter provides a more C++-friendly interface.
As for the errors, most likely:
Your include path or link library path is wrong if it's failing at compile-time or link time; or
Your PATH doesn't include libpq, if it's failing at runtime.
Most likely (you didn't provide any actual error messages) the problem is your setup. You need to add the correct header ("include") directory and library directory; if you just copied them from some tutorial, make sure they actually point to the real place where these files (.h and .lib files, respectively) are.
You need do recompile the libpq with MSVC (nmake /f win32.mak all), after that, you need to set your project to use that library, and change the Common Language Runtime Support to (/clr) and not to clr/pure.

How to generate vcproj files?

Suppose I've got a cross-platform C++ library, let's call it ylib. My primary development platform is Linux. I'd like ylib to be buildable by MSVC (2008, 2010, 11, etc).
What's the best way to do this? Do I generate .vcproj files? If so, how? Ideally I'd be able to generate them without using Windows myself. No extra dependencies should be required for Windows.
Multiple variants should be build: debug dll, debug lib, release dll, release lib with dynamic runtime and release lib with static runtime.
You could use cmake for your build scripts. Cmake has the ability to generate visual studio project files from the cmake build scripts. So you'd just need to distribute your cmake files, then individual people using windows could generate MSVC project files from that.
Though as pointed out in the comments, it'd be difficult to guarantee that you could actually build your project under visual studio without trying it out yourself.
EDIT: Though I've just realized that you requested no extra dependencies on linux, which this would not solve. Unless you generated the vcproj files yourself using cmake, and distributed them. But personally I think it'd be better to just have the cmake dependency. It's freely available, and easy to install.
This also has the advantage of supporting whatever version of visual studio your end user happens to have, without the need for distributing several different formats.
You just need to understand the format of vcproj files and then write them - they are simply XML.
I don't know how well MSFT document the settings (not very if history is a guide) - so for simple projects I would just create a project in MSVC and look at what it writes.
Alternatively you could just use cmake which already does this.

Setting up midiIO library on Windows

I've downloaded the midiIO library and in the readme it says:
edit the file Makefile.library and set the OSTYPE and OSSUBTYPE to match your hardware/os setup.
type "make library" to compile the library. It will be created as lib/libmidiio.a in unix.
edit the file Makefile.examples and set the OSTYPE and OSSUBTYPE to match your hardware/os setup.
Also, if you are using ALSA, then uncomment out the POSTFLAG to use the alsa library (-lasound).
type "make examples" to compile the example programs in the examples directory. The example programs will be place in the bin directory.
1 + 3 are fine but 2 + 4 are over my head. I've worked in a unix environment before and have used gcc with flags but I need to get this done in Windows. I typically use Visual Studio but don't know how to achieve this with that.. I've downloaded Dev-C++ if that's any use but I don't know what to do with the makefiles?
Ignore the makefiles and set up a fresh project in Visual C++. Make your target a static library (which will be a .lib file in Win32, not a .a file as in unix as you probably know). It is unlikely that the project will build out of the box, so you might have to deal with some compilation errors relating to unix-specific symbols. I took a quick look at the source code, and it looks fairly well-written, so I don't think you should have many problems building it directly in Windows.
Alternately, you could build the source using the real make tool in cygwin, but this means that you would need to distribute the cygwin library with your final product. This may or may not be more trouble than it's worth, especially if you are already using VC++ for the rest of your project's code.