MS link fails from gnu make, but works from cmd line - c++

Recently my gnu makefile stopped linking my C++ project. I had made some changes. I have copied the link line out and run it from a batch file. It builds fine. But the same line strangely fails when I run make. The error it gives is:
LINK : fatal error LNK1181: cannot open input file 'user32.lib'
which has to be a red herring because from the same command line prompt, running the link command succeeds. I am beginning to suspect GNU make. This used to work from within make but I made a few additions and changes to the makefile to get it building on Linux which seemed to introduce the problem.
I am using :
GNU Make 3.80
MS Visual C++ Linker 10.00.40219.01
on Windows XP.
My LIB and LIBPATH both include the path to the SDK directory which contains the libraries. My link command is as follows:
link C:\SDL-1.2.14\lib\SDL.lib C:\SDL-1.2.14\lib\SDLmain.lib C:\SDL-1.2.14\lib\SDL_image.lib C:\SDL-1.2.14\lib\SDL_ttf.lib C:\SDL-1.2.14\lib\SDL_mixer.lib C:\SDL-1.2.14\lib\SDL_net.lib ../../build/lib/sdlhal.lib user32.lib gdi32.lib kernel32.lib oldnames.lib wsock32.lib advapi32.lib comdlg32.lib comctl32.lib wsock32.lib winmm.lib netapi32.lib OpenGL32.lib glu32.lib /nologo /incremental:no -subsystem:console /PDB:../../build/bin/Prog.pdb /OUT:../../build/bin/Prog.exe /MAP:../../build/bin/Prog.map ../../build/Prog/intr/util.obj ../../build/Prog/intr/objwithvel.obj ../../build/Prog/intr/rock.obj ../../build/Prog/intr/explosion.obj ../../build/Prog/intr/ship.obj ../../build/Prog/intr/photon.obj ../../build/Prog/intr/world.obj ../../build/Prog/intr/test.obj ../../build/Prog/intr/main.obj
EDIT: Bit of progress with C++ program below. My Environment variables were:
LIB=C:\Program Files\Microsoft Visual Studio 10.0\VC\LIB;C:\Program Files\Microsoft Visual Studio 10.0\VC\ATLMFC\LIB;C:\Program Files\Microsoft SDKs\Windows\v7.0A\lib;
LIBPATH=C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319;C:\WINDOWS\Microsoft.NET\Framework\v3.5;C:\Program Files\Microsoft Visual Studio 10.0\VC\LIB;C:\Program Files\Microsoft Visual Studio 10.0\VC\ATLMFC\LIB;
But when printed out by program run from within make, they are:
LIB=.lib
LIBPATH=C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319;C:\WINDOWS\Microsoft.NETFramework\v3.5;C:\Program Files\Microsoft Visual Studio 10.0\VC\LIB;C:\Program Files\Microsoft Visual Studio 10.0\VC\ATLMFC\LIB;

Based on your description of the value of LIB, I suspect that you have simply set the "LIB" variable in your makefile (to ".lib"). In standard GNU make (actually, all versions of make) all the environment variables are imported into make as make variables when make starts up (there are exceptions, such as SHELL, which are handled differently).
Whenever make invokes a command, all the current values of variables that were imported are written out to the environment of the child process.
Put another way, any variable that make read from its environment is considered to be marked for export when make runs any command from a recipe.
So if you have a makefile like this:
LIB = .lib
all: ; #echo "LIB = %LIB%"
and you run it like this:
> set LIB=C:\foo;C:\bar
> make
then the output will be "LIB = .lib", not "LIB = C:\foo;C:\bar"

I suspect that your LIB and LIBPATH variables aren't set the way you think they are when link is invoked from make.
Try creating your own link.exe program that gets invoked from make and dumps the command line and environment. That'll tell you for certain whether or not the variables are set correctly by the makefile.
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char** argv, char** env)
{
int i = 0;
for (i = 0; i < argc; ++i, ++argv) {
if (!(*argv)) {
*argv = "(null)";
}
printf( "arg[%d]: \"%s\"\n", i, *argv);
}
puts("\nENVIRONMENT...\n");
while (*env) {
printf( "%s\n", *env);
++env;
}
return 0;
}
Also check that there aren't unquoted spaces in the wrong place in the link command line. While it would seem to be unrelated to the error you mention in the question (and is probably really just a cut/paste error), the link command line you gave has a space in the middle of the path/file name for explosion.obj.

This is likely to be a path issue. Are you using the mingw version of GNU make, or the Cygwin version?
One way to debug this is to put an echo $LIB and echo $LIBPATH command into the make recipe to confirm that these are getting set as you expect.

Related

Error code = 0x80070002 in visual studio c++

I just installed visual studio, (not new to coding) and I keep getting an error that goes like "error: unable to open file C:user...main.obj Error code = 0x80070002"
an Image of the error
this error shows up whenever there is something wrong with my code like for example if i do:
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
}
everything runs normal, but if I make a mistake say:
#include <iostream>
int main()
{
std::cout std::cout << "Hello World!\n";
}
I get the error, it's so annoying I don't want to get that error every time there is something wrong with my code the error list bar is enough, what do I do ?
Could you please tell us what version of Visual Studio are you using?
If you are using vs2017 or later, I suggest you could try to use /DEBUG:FULL property in "Linker/Debugging/Generate Debug Info.
I suggest you could refer to the Doc:
When you specify /DEBUG with no additional options, the linker
defaults to /DEBUG:FULL for command line and make file builds, for
release builds in the Visual Studio IDE, and for both debug and
release builds in Visual Studio 2015 and earlier versions. Beginning
in Visual Studio 2017, the build system in the IDE defaults to
/DEBUG:FASTLINK when you specify the /DEBUG option for debug builds.
Other defaults are unchanged to maintain backward compatibility.
Rebuild the program in a different disk or reinstall the VS could be the final solution I think.

"CL.exe" Not Compiling C++ in stand alone project

How to fix "CL.exe" Not Compiling C++??
I have created a standalone python project on my desktop, keep in mind that I'm not using Visual Studio, I am in fact using pythontowin. I would like to compile a C++ file into a DLL to use for my python project. But I am getting weird unexplained errors when trying to compile with cl.exe.
Here is my C++ file.
#include <Windows.h>
#include <iostream>
using namespace std;
void Thread() {
cout < "Hello World" < endl;
Sleep(1000);
}
BOOL DllMain(HINSTANCE hDll,DWORD dwReason, LPVOID) {
if (dwReason == DLL_PROCESS_ATTACH) {
CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Thread, 0, 0, 0);
}
return TRUE;
}
Command used for cl.exe: cl.exe /std:c++14 /LD ./../LibraryXdll.cpp
And this is the error I am getting: fatal error C1034: Windows.h: no include path set
If anyone could solve this error I would be very thankful and I can finish off my project thanks for the help. Regards RanOutOfQuestions!
Taking up on Tadman's comment:
It looks like you didn't set up your environment variables for use of cl.exe
If you have gotten cl.exe, you should also have a Bat-File called vcvarsall.bat or vcvars32.bat / vcvars64.bat. In case of a default Visual studio installation these can be found under "Program Files (x86)\Microsoft Visual Studio\version number\produkt type, e.g. community\VC\Auxiliary\Build".
Run the appropriate version of vcvars... (32 or 64 bit, or specifiy the flag for vcvarsall, see docs), and your include paths for THAT SESSION will be set (inside that terminal, if you close it the changes are lost again and you have to call vcvars again next time).
EDIT: Microsoft documentation on vcvars
You can work around that Reset by calling vcvars every time before executing the compiler, though this isn't a beautiful solution

Can't include mysql

I have been trying for a while now. I have been asked to do a few modifications on a big program, and it requires a lot of libs. All of them were succesfully added, except mysql.
I downloaded the libs several times and tried a lot of times.
I am on Windows using Netbeans and C++.
The test I decided to do is simple: create an empty program and try to include mysql. I didn't have any success:
#include <cstdlib>
#include <mysql/mysql.h>
using namespace std;
int main(int argc, char** argv) {
return 0;
}
This program won't work even if I replace #include <mysql/mysql.h> with #include <mysql.h>.
The error NetBeans gives me is the following:
main.cpp:15:25: fatal error: mysql/mysql.h: No such file or directory
These images show how I altered the project options on Linker and C/C++ options:
Parameters
The folder showing on the C image does contain a mysql.h, i triple checked twice.
I also have added the paths with includes to CodeAssistance.
Many similar questions were left unanswered for years now on several forums, even on stackoverflow, and I can't seem to work this out. This thread may have a final answer to those of us who stumble upon this.
It is not able to find the file
At this point, your program would be pointing to something like this:
cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"
mysql1.c
So you needed to include the directory that contains mysql.h as well. So the above would changed to:
cl /I "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include"
/I "C:\Program Files\MySQL\MySQL Server 5.5\include" mysql1.c
verify the header file exist in the path /usr/include/mysql/mysql.h or installed some where else. If you have installed the header files somewhere else ( add that location with -I/.

Cannot run Hello World C++ program

I am just starting C++ and downloaded a compiler and an IDE, both eclipse, and tried to make my first C++ program. I use the Hello World C++ Makefile Project, and add the all the stuff on the next page. I then build the program, and the build says this:
12:30:00 **** Build of configuration Default for project HelloWorld! **** make all Cannot run program "make": Launching failed
Error: Program "make" not found in PATH PATH=[C:/Program Files
(x86)/Java/jre1.8.0_91/bin/client;C:/Program Files
(x86)/Java/jre1.8.0_91/bin;C:/Program Files
(x86)/Java/jre1.8.0_91/lib/i386;C:\ProgramData\Oracle\Java\javapath;C:\Program
Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files
(x86)\Common Files\Microsoft Shared\Windows
Live;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;c:\Program
Files (x86)\ATI Technologies\ATI.ACE\Core-Static;c:\Program Files
(x86)\Common Files\Roxio Shared\DLLShared\;c:\Program Files
(x86)\Common Files\Roxio Shared\12.0\DLLShared\;C:\Program Files
(x86)\Windows Live\Shared;c:\Program Files (x86)\Microsoft SQL
Server\100\Tools\Binn\;c:\Program Files (x86)\Microsoft SQL
Server\100\DTS\Binn\;C:\Users\Jen\Desktop\eclipse;] 12:30:00 Build
Finished (took 122ms)***
I run the program, and it says:
Launch failed. Binary not found.
This is the code for the program:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("Hello World!");
return EXIT_SUCCESS;
}
There are two errors, too:
Function "puts" couldn't be resolved.
Symbol "EXIT_SUCCESS" couldn't be resolved.
Thanks in advance!
Nate N.
/////////////////////////////////////////////////////////////////////////
I followed the advise of user4581301 and the code now says this:
#include <iostream>
using namespace std;
int main() {
cout << "Hi World" << endl; // prints Hi World
return 0;
}
The build looks like this:
13:22:26 **** Rebuild of configuration Debug for project HiWorld ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\HiWorld.o" "..\src\HiWorld.cpp"
g++ -o HiWorld.exe "src\HiWorld.o"
13:22:27 Build Finished (took 835ms)
But now the program, although it does run, doesn't print anything.
Thanks in advance,
Nate N.
Launch failed. Binary not found.
well this is shown because you have 2 errors.
any error means = no binary compiled/linked
no binary compiled/linked = cant launch something which doesnt exist
solution = fixing the errors
#include #include
^ also this is not valid
Cannot run program "make": Launching failed Error: Program "make" not found in PATH
Kaboom! No make, no build, no binary to run.
Some compilers don't ship with Make out of the box, but I don't see any signs of a compiler toolchain in your path at all.
Eclipse is an IDE with support for a wide variety of different compilers. as a result it does not ship with one. Since the error message shows you are building on Windows, your simplest option is one of the many MinGW variants. And Since you are just starting, I recommend starting off with the plain-vanilla MinGW as Eclipse requires next to no extra configuration to use it.
Just make sure you follow the instructions to install msys along with MinGW to get make.

Cannot run program makefile when compiling a c++ program with NetBeans and MinGW gcc

I'm trying to compile a simple "Hello" program on Windows 7 x64:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout << "Hello" << endl;
return 0;
}
And getting an error in the NetBeans IDE when compiling:
CLEAN SUCCESSFUL (total time: 126ms)
D:\GDrive\CS\Cpp\NetBeans_Cpp\TNB_cpp\Tut1_10_HelloWorld\Makefile -f nbproject/Makefile-Debug.mk build/Debug/MinGW-Windows/main.o
Cannot run program "D:\GDrive\CS\Cpp\NetBeans_Cpp\TNB_cpp\Tut1_10_HelloWorld\Makefile" (in directory "D:\GDrive\CS\Cpp\NetBeans_Cpp\TNB_cpp\Tut1_10_HelloWorld"): CreateProcess error=193, %1 is not a valid Win32 application
COMPILE FILE FAILED (exit value -2, total time: 16ms)
My PATH env variable looks like this:
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;F:\Program_Files\JDK\bin;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;F:\Program_Files\MinGW\bin;F:\Program_Files\MinGW\msys\1.0\bin;
The last two varibles being probably the most important since they pertain to the MinGW. My ideas is that something is missing in the PATH, but maybe its not where the problem hiddes.
Thanks.
It looks as if some settings are wrong.
Look at your Netbeans settings : Toolchain : Make Command
looking after one of the files.
make.exe
mingw32-make.exe.
In the directories, for example,
c:\msys\1.0\bin
c:\MinGW\bin
goto -> options -> c/c++ -> Build Tools
and check if the settings match this