-1

I run into the following error when using make. So, I compiled the same program at the command line to check if there was a problem in my C++ code, but it works perfectly fine at the commandline.

g++ -o Cmain.exe Cmain.o -lmysqlclient -lboost_date_time -L ../lib -L/usr/local/lib -L/usr/lib/mysql 
./Cmain.exe
terminate called after throwing an instance of 'std::logic_error'
      what():  basic_string::_S_construct NULL not valid
    make: *** [Cmain.exe] Aborted
    make: *** Deleting file `Cmain.exe'

Any suggestions on what might be causing this error and how it can be fixed.

itcplpl
  • 780
  • 4
  • 18
  • 29

3 Answers3

4

Edit from the edited question, it now became clear that CMain.exe is not being executed, but rather being built.

The same story applies (see below), but I'll post more relevant hints once there is more new information. Hang on


I can bet 80% that the program calls std::getenv

getenv(...)

and doesn't check the return value of null (0). If you then construct a std::string from it the reported exception is expected.

Search your source code for getenv(...) and replace code like, say:

 std::string s = getenv("SOMEVAR");   // WRONG!

by, e.g.

 const char* raw = getenv("SOMEVAR");
 std::string s = raw?raw:"";         // RIGHT!

You can then use s.empty() to see whether the variable had a value (you can no longer see whether it existed in the environment because std::string can not represent 'null' - only the empty string).


PS.: Background/off-topic:

I'd bet that the environment is different because environment differences are frequently cause of trouble/confusion with make engines (CMake, nmake, SCons etc.) Especially SCons is known to be very strict by default: it doesn't even add a $HOME variable to the environment, which break e.g. ccache/ccontrol setups.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I am running a SELECT query and am connecting to MySQL. Am working on Ubuntu. A google search also suggested $ mv .mysqlgui .mysqlgui~ but I get an error that no such file or directory exists. I wonder if this is a MySQL connectivity problem... – itcplpl Sep 18 '11 at 20:01
  • I am still a newbie working with makefiles. when you suggest modifying the makefile to launch CMain.exe with a debugger, ..how do I go about it. any code help would be appreciated – itcplpl Sep 18 '11 at 20:19
  • @itcplpl: you need to post more of your make file + environment. Right now I have to GUESS that you use g++ with libstdc++6 (because I think I recognize the message details?) but I'm left guessing whether that is on Windows (mingw/cygwin?), Linux, Solaris, AIX? – sehe Sep 18 '11 at 20:24
  • sorry, I just edited my code to add the output from the makefile before the error. I use Linux, and on the commandline, I had: $g++ Cmain.cpp -o cc -L/usr/lib/mysql -lboost_date_time -lmysqlclient – itcplpl Sep 18 '11 at 20:37
  • @itcplpl: so it is by definition an environmental difference. I'd check what g++ resolves to from the command line: ls -ltrad `which g++`. – sehe Sep 18 '11 at 20:39
  • this is what it returns $ls -ltrad drwxr-xr-x 51 ba ba 4096 2011-09-18 15:09 . $which g++ /usr/bin/g++ – itcplpl Sep 18 '11 at 20:47
  • so if I understand correctly, I should run rm Cmain.exe && make - wnp Cmain.exe – itcplpl Sep 18 '11 at 20:59
  • I just ran it, towards the end I have: # No general (`VPATH' variable) search path. # # of strings in strcache: 2 # # of strcache buffers: 1 # strcache size: total = 4096 / max = 4096 / min = 4096 / avg = 4096 # strcache free: total = 4075 / max = 4075 / min = 4075 / avg = 4075 # Finished Make data base on Sun Sep 18 17:01:36 2011 – itcplpl Sep 18 '11 at 21:06
  • @itcplpl: as you might guess, that snippet is not showing any useful information. (sorry about the mistaken program name earlier) – sehe Sep 18 '11 at 21:07
  • enclosed please find link..http://pastebin.com/8tfP9pkR – itcplpl Sep 18 '11 at 21:25
  • Is there a g++ in /usr/local/bin? I see no really strange signs (a couple of good signs :)). I'm afraid I'm not going to be able to say anything sensible from here. Not unless you can post a minimal failing sample... I imagine by the time you achieved that, you'll have solved it. Sorry I can't be of more help – sehe Sep 18 '11 at 21:41
  • it's nice to know about the good signs :). if I understand your q? correctly regd. g++, that's what the 'which g++' stated. I am quite clueless at this point how to create the minimal failing sample when it compiles and executes fine from the commandline :(...the only alternative would be to post the entire code, which I doubt I can do with just 1 program...but thanks for looking at it so far. – itcplpl Sep 18 '11 at 21:49
  • @itcplpl: well posting a tarball should not be difficult: it is more a question of whether you want to share the information :) I really meant `Is there a g++ in /usr/local/bin`. So, `ls -ltrad /usr/loca/bin/*` :) (_PS. good sign: CWEAVE, CMain.d auto dependency mgmt and tidbits like that. Just heuristics from/about experience, really_) – sehe Sep 18 '11 at 21:53
  • actually, I will admit that I will have to get permission to share the info. aha, I would have not been able to make much sense from that :-). to your point regd g++, I just ran ls -ltrad /usr/local/bin/* ls: cannot access /usr/local/bin/*: No such file or directory so I did, cd /usr/local/bin and did an ls, and it shows nothing...did I miss something. – itcplpl Sep 18 '11 at 22:39
  • Ltcplpl no that is just good confirmation that the same g++ is being found & used. Like I said before, that leaves me out of optionts to persue for the moment – sehe Sep 18 '11 at 22:54
  • I think the problem was as you identified correctly. fixed that with your code for getenv. however, now it runs the program, and at the end after displaying the results correctly, prints the message: make: *** [Cmain.exe] Error 1...any clue why? – itcplpl Sep 19 '11 at 22:18
  • Hmmm. Is CMain.exe being executed after all. Glad my psychic debugging skills are still working (hahaha!). Well, to continue my psychic debugging tour, I suspect that the same program is exiting with `exit(1)` or `return 1` from main? – sehe Sep 19 '11 at 22:20
  • haha, yes indeed it was executed. and yes, it is exiting with return 1 from main. – itcplpl Sep 19 '11 at 22:37
  • 1
    @itcplpl: You'll find that returning `0` won't have the same tendency to upset make's day. Alternatively, you can combine two commands in the makefile, say `./bin/CMain.exe -o options; true` instead of just `./bin/CMain.exe -o options` (_rationale: commands are executed line-by-line in subshells. Make will abort the target if any one commandline (<--) exits with non-zero status_) – sehe Sep 19 '11 at 22:41
  • wow, you're absolutely right - that did work. understood, thanks a bunch :-) – itcplpl Sep 19 '11 at 22:47
  • any hunch on why in an iterative loop (basically a goal seek), if I include a print statement in the middle of the loop it runs, otherwise it hangs. I am baffled that something like this could happen but I've run it several times before posting this note and the behavior is repeated. – itcplpl Sep 21 '11 at 14:48
  • @itcplpl: what? I became your personal [oracle](http://thesaurus.com/browse/augur) then :_ I predict you get best results when you (a) post the code as a question on SO (b) I predict my answer would be: **[UB](http://stackoverflow.com/questions/2766731/what-exactly-do-ib-and-ub-mean)**, use valgrind to find out where. Cheers – sehe Sep 21 '11 at 15:09
  • hahaha...it seems to be UB, I'll check out valgrind...cheers :-) – itcplpl Sep 21 '11 at 15:12
2

It's a std::string trying to be constructed from a NULL pointer.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
-2

It's a problem in the make program itself. Report it to the program's author.

zvrba
  • 24,186
  • 3
  • 55
  • 65