C++ Compile error on NetBSD '::system' has not been declared - c++

I have some C++ code that compiles nicely on Linux but so far I'm having trouble getting it to compile correctly on NetBSD.
These are my includes:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <iomanip>
#include <boost/regex.hpp>
I compile with this command:
g++ -v -O2 -fno-strict-aliasing -I /usr/pkg/include \
barefoot.cpp -o barefoot -L /usr/pkg/lib/ -lboost_regex \
-L /usr/pkg/lib/ -lboost_regex -lpthreads
Which renders this output:
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64--netbsd
Configured with: /usr/src2/tools/gcc/../../external/gpl3/gcc/dist/configure --target=x86_64--netbsd --enable-long-long --enable-threads --with-bugurl=http://www.NetBSD.org/Misc/send-pr.html --with-pkgversion='NetBSD nb2 20111202' --enable-__cxa_atexit --with-tune=nocona --with-mpc=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-mpfr=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-gmp=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --enable-tls --disable-multilib --disable-symvers --disable-libstdcxx-pch --build=x86_64-unknown-netbsd5.99.56 --host=x86_64--netbsd
Thread model: posix
gcc version 4.5.3 (NetBSD nb2 20110806)
COLLECT_GCC_OPTIONS='-v' '-O2' '-fno-strict-aliasing' '-I' '/usr/pkg/include/' '-o' 'a.out' '-L' '/usr/pkg/lib/' '-mtune=nocona' '-march=x86-64'
/usr/libexec/cc1plus -quiet -v -I /usr/pkg/include/ barefoot.cpp -quiet -dumpbase barefoot.cpp -mtune=nocona -march=x86-64 -auxbase barefoot -O2 -version -fno-strict-aliasing -o /var/tmp//cc9Dcmwi.s
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
#include "..." search starts here:
#include <...> search starts here:
/usr/pkg/include/
/usr/include/g++
/usr/include/g++/backward
/usr/include/gcc-4.5
/usr/include
End of search list.
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
Compiler executable checksum: a34e7d170f4dd8d4687d2b62e8dca4b7
In file included from /usr/include/g++/bits/gthr.h:166:0,
from /usr/include/g++/ext/atomicity.h:34,
from /usr/include/g++/bits/ios_base.h:41,
from /usr/include/g++/ios:43,
from /usr/include/g++/istream:40,
from /usr/include/g++/fstream:40,
from barefoot.cpp:29:
/usr/include/g++/bits/gthr-default.h:130:9: error: 'pthread_detach' was not declared in this scope
/usr/include/g++/bits/gthr-default.h:130:1: error: invalid type in declaration before ';' token
/usr/include/g++/bits/gthr-default.h: In function 'int __gthread_detach(pthread_st*)':
/usr/include/g++/bits/gthr-default.h:668:46: error: '__gthrw_pthread_detach' cannot be used as a function
In file included from /usr/pkg/include/boost/regex/v4/regex_workaround.hpp:25:0,
from /usr/pkg/include/boost/regex/v4/regex.hpp:32,
from /usr/pkg/include/boost/regex.hpp:31,
from barefoot.cpp:32:
/usr/include/g++/cstdlib: At global scope:
/usr/include/g++/cstdlib:132:11: error: '::system' has not been declared
I tried adding -lpthread, but that resulted in the same output. Not sure what to try next.

I do not know exactly why you are facing this problem, but I can provide you with some next steps to try.
Notice that the first error is that pthread_detach is missing. The system header files will typically make sure whatever headers they need are included, but you can try adding <pthread.h> above all the other headers.
If that fails, you need to figure out which file got included as a result of adding #include <pthread.h>, and make sure pthread_detach is present inside it (it really should be there somewhere).
Assuming it is there, there must be some conditional compilation that is causing it to not be visible to your source code. Find the conditional guards, and what macro values affect it.
Then, you will need to see why your system is defining the macro values in a way that causes the function pthread_detach to not be visible.
With this investigation, you may discover that you are not including the right <pthread.h> file. This may be due to system include directories conflicting with the include directories you want to use.
This answer (it is towards the bottom) to this question shows how to display the predefined macros for the g++ compiler.

Related

Can't compile a simple c++ file with g++ on MacOS 13, XCode 14

I'm on a new M2 Mac Mini. I have this simple test c++ file:
% cat conftest.cpp
#include <limits>
#include <cmath>
#include <algorithm>
int main(int argc, char **argv) { return 0;}
I need to compile it with c++ rather than clang++ (the actual problem is building a conan dependency that does this, so I can't easily switch it).
I have XCode 14.2 (latest, I think) and this c++
% g++ --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
From my reading, I know that since Catalina XCode's g++ doesn't automatically find the standard header and lib dirs the way clang does on Mac, so I think I need to set CPATH and LIBPATH.
% echo $LIBRARY_PATH
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
% echo $CPATH
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
Unfortunately, compiling this gives an error, which appears to be a conflict between cmath and algorithm because removing either of those makes it compile and link OK:
% /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ conftest.cpp
In file included from conftest.cpp:4:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:653:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:499:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional/bind_back.h:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional/perfect_forward.h:17:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:159:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_base:22:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/exception:83:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstdlib:130:9: error: target of using declaration conflicts with declaration already in scope
using ::abs _LIBCPP_USING_IF_EXISTS;
^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h:132:6: note: target of using declaration
int abs(int) __pure2;
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:338:1: note: conflicting declaration
using ::abs _LIBCPP_USING_IF_EXISTS;
^
I must be missing something simple. That test program compiles and runs fine on godbolt and Windows. Any ideas, anyone?
The issue here is one of wrapping. When you invoke the toolchain compiler directly, it uses a specific set of headers for the toolchain itself, and omits the headers for the platform. When we use -### to see all the include paths specified for the toolchain compiler, we see the following from the direct toolchain compiler:
So compiling with /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++:
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1"
"/usr/local/include"
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include"
"/usr/include"
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"
Compiling with the /usr/bin/c++ wrapper, we get the following headers:
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
"-I/usr/local/include"
"-stdlib=libc++"
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include"
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include"
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include"
It's adding in the headers for macOS, because it assumes it's building for MacOS, because it's the local wrapper.
We can successfully compile using the toolchain compiler, when we pass in an additional -isysroot option as extracted using:
-isysroot $(xcrun --sdk macosx --show-sdk-path)
so:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot $(xcrun --sdk macosx --show-sdk-path) -o conftest conftest.cpp
builds and runs cleanly. In the case of trying to get dependencies to build properly you're going to need to pick the appropriate compiler for the target.
If you want to work around building, the two variables that matter in this case are generally CPPFLAGS and CXXFLAGS - flags for the preprocessor and C++ compiler. You should be able to set CPPFLAGS to:
-isysroot $(xcrun --sdk macosx --show-sdk-path)
and it should build for you, if the standard makefile logic is in effect in the tools you're using.
$ export CPPFLAGS="-isysroot $(xcrun --sdk macosx --show-sdk-path)"
$ export CXX=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
$ make conftest
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk conftest.cpp -o conftest

What causes g++ ignoring nonexisting directory

I am learning to use make. I am trying to compile a program using g++ on my home machine that I have successfully compiled using xl on a school machine. I am trying to link with an external library that I built and placed in a separate directory. I am trying to include the header file, but g++ claims the include directory does not exist. The include directory definitely exists, tab complete fills it in and find locates it as well.
Why does g++ decide the directory does not exist? In general what causes a directory to be ignored? I want to know why this is breaking so I can better write makefiles. More importantly to me, how do I fix this and have g++ search for arbitrary header files in whatever directory I tell it to look in?
I have seen questions about g++ ignoring directories, but those were in the case of ignoring various system default locations. They usually solved their problem by removing a conflicting version or just re installing their tool. My problem is different because g++ is ignoring a user defined directory that actually exists.
src/a3.cc:1:17: fatal error: apf.h: No such file or directory
#include "apf.h"
$ find /home/USER/Documents/GitHub/core/apf/
/home/USER/Documents/GitHub/core/apf/
/home/USER/Documents/GitHub/core/apf/apfUserData.cc
....
/home/USER/Documents/GitHub/core/apf/apf.h
....
I have tried manually specifying the directory in the make file using -I
apf = /home/USER/Documents/Github/core/apf/
CFLAGS = $(CFLAGS) -Wall -g -I $(apf) --pedantic-errors --verbose
I have also tried setting CPLUS_INCLUDE_PATH
export CPLUS_INCLUDE_PATH = $(apf)
For each of these attempts I see the same error, that g++ is "ignoring nonexistant directory"
COLLECT_GCC=/usr/bin/g++
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/home/USER/Documents/Github/core/apf/"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.9.2/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../x86_64-redhat-linux/include"
ignoring duplicate directory "/usr/local/include"
as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
./inc
/usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../include/c++/4.9.2
/usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../include/c++/4.9.2/x86_64-redhat-linux
/usr/lib/gcc/x86_64-redhat-linux/4.9.2/../../../../include/c++/4.9.2/backward
/usr/lib/gcc/x86_64-redhat-linux/4.9.2/include
/usr/local/include
/usr/include
End of search list.
GNU C++ (GCC) version 4.9.2 20150212 (Red Hat 4.9.2-6) (x86_64-redhat-linux)
compiled by GNU C version 4.9.2 20150212 (Red Hat 4.9.2-6), GMP version 6.0.0, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 954ba53e83b294b646fa1b6b73a69682
It appears that you have a case sensitive file system. You have GitHub in some places and Github in others. Make them all match correctly and you'll be in better shape.

Getting GCC in C++11 mode to work on FreeBSD

How do I get a working GCC-based C++11 setup on FreeBSD 10? It seems that the standard library that comes with recent GCC versions on FreeBSD is broken. I've installed the port gcc49 and then try to compile this:
#include <string>
int main()
{
auto str = std::to_string(42);
str = std::to_string(42ull);
str = std::to_string(4.2);
str.clear();
return 0;
}
This gives me an error:
g++49 -v -std=c++11 foo.cc
Using built-in specs.
COLLECT_GCC=g++49
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/lto-wrapper
Target: x86_64-portbld-freebsd10.0
Configured with: ./../gcc-4.9-20141022/configure --disable-nls --enable-gnu-indirect-function --libdir=/usr/local/lib/gcc49 --libexecdir=/usr/local/libexec/gcc49 --program-suffix=49 --with-as=/usr/local/bin/as --with-gmp=/usr/local --with-gxx-include-dir=/usr/local/lib/gc
c49/include/c++/ --with-ld=/usr/local/bin/ld --with-pkgversion='FreeBSD Ports Collection' --with-system-zlib --with-ecj-jar=/usr/local/share/java/ecj-4.5.jar --enable-languages=c,c++,objc,fortran,java --prefix=/usr/local --mandir=/usr/local/man --infodir=/usr/local/info/gcc49 --build=x86_64-portbld-freebsd10.0
Thread model: posix
gcc version 4.9.2 20141022 (prerelease) (FreeBSD Ports Collection)
COLLECT_GCC_OPTIONS='-v' '-std=c++11' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/local/libexec/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/cc1plus -quiet -v foo.cc -quiet -dumpbase foo.cc -mtune=generic -march=x86-64 -auxbase foo -std=c++11 -version -o /tmp//ccbNFhtI.s
GNU C++ (FreeBSD Ports Collection) version 4.9.2 20141022 (prerelease) (x86_64-portbld-freebsd10.0)
compiled by GNU C version 4.9.2 20141022 (prerelease), GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/../../../../../x86_64-portbld-freebsd10.0/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/lib/gcc49/include/c++/
/usr/local/lib/gcc49/include/c++//x86_64-portbld-freebsd10.0
/usr/local/lib/gcc49/include/c++//backward
/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/include
/usr/local/include
/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/include-fixed
/usr/include
End of search list.
GNU C++ (FreeBSD Ports Collection) version 4.9.2 20141022 (prerelease) (x86_64-portbld-freebsd10.0)
compiled by GNU C version 4.9.2 20141022 (prerelease), GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 8405316ee381c37148f55a17e42da47a
foo.cc: In function 'int main()':
foo.cc:5:14: error: 'to_string' is not a member of 'std'
auto str = std::to_string(42);
^
foo.cc:6:9: error: 'to_string' is not a member of 'std'
str = std::to_string(42ull);
^
foo.cc:7:9: error: 'to_string' is not a member of 'std'
str = std::to_string(4.2);
^
The default include search path is /usr/local/lib/gcc49/include/c++/, which does contain the string header. Makes sense, because the gcc49 port ships with a corresponding libstc++. But it seems broken, per the above error.
Is this a known problem? Has anyone gotten a working setup of a GCC-based toolchain for C++11 on FreeBSD?
(Note: I'm aware that Clang is the default compiler on FreeBSD 10. But in this case I'm specifically looking to support a GCC-based toolchain.)
Turns out that adding the following flags to g++ will make it work:
-D_GLIBCXX_USE_C99 -D_GLIBCXX_USE_C99_MATH -D_GLIBCXX_USE_C99_MATH_TR1

‘cout’ is not a member of ‘std’ & ‘cout’ was not declared in this scope

I realize that there are several duplicates like this but none of them have worked for me so far.
I am trying to compile a C++ very simple program on Ubuntu using g++ but it is giving me scope errors.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
This gives me this error:
sudo g++ -v test.c
test.c: In function ‘int main()’:
test.c:7:3: error: ‘cout’ was not declared in this scope
I also tried defining the scope as many other posts say, but that also didn't work, but gave me a different error:
#include <iostream>
int main()
{
std::cout << "Hello world";
}
Gives error:
test.c: In function ‘int main()’:
test.c:6:3: error: ‘cout’ is not a member of ‘std’
Most of the suggestions online suggest "using namespace std;", "#include " and "std::cout".
So I tried all 3 together, still no luck :(
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello world";
}
gives error:
test.c: In function ‘int main()’:
test.c:7:3: error: ‘cout’ is not a member of ‘std’
I have gone through several forums online but none of them seem to work for me :(
This is a part of a bigger issue because of which one of my linux make doesn't work.
Btw, I am using g++ and not gcc as a few posts messed up.
EDIT 1:
I changed the name to .cpp, and execute without sudo. Here is the verbose output:
pranoy#pranoyubuntu1210:~/Desktop/SIP/SIPp/sipp-3.3$ g++ -v test.cpp -o test
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)
COLLECT_GCC_OPTIONS='-v' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.7/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -version -fstack-protector -o /tmp/cczzibvL.s
GNU C++ (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu)
compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu)
compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 521527ea42f0901bf839bcaad0cb13e6
test.cpp: In function ‘int main()’:
test.cpp:5:3: error: ‘cout’ is not a member of ‘std’
This isn't the case for this, but could be an answer to "error: ‘cout’ is not a member of ‘std’". I came here looking for help, so this might help someone else.
If you have a header file (header.h) where the last function/class declaration does not have a semi-colon after it:
#include <string>
function(std::string str)
And your c++ file includes this before iostream:
#include "header.h"
#include <iostream>
The function/class (function in this case) messes up the declarations in iostream. #include basically just pastes the text into the code. Adding the semi-colon can fix the problem.
What you've written works absolutely fine on my Ubuntu system, with the same version of g++.
It sounds like you haven't installed all of the necessary files for the C++ environment, or something isn't quite right with it. Try this:
$ sudo apt-get remove g++ libstdc++-6.4.7-dev
$ sudo apt-get install build-essential g++-multilib
(Run dpkg -l | grep libstdc++ to get the exact version of libstdc++ if the above fails)
For C++ programs, use g++, not gcc. If you get errors that suggest your compiler cannot find the standard library, that's because you probably used gcc.
(C++, Linux Terminal, no compiling error, but no prints into the Terminal), maybe try:
$ g++ yourcode.cpp
$ ./a.out
first line: g++ compiles your code.
second line: runs your compiled code.
Rename file extension with .cpp .
Compile source code by command
gcc source.cpp -o output.out
GCC will automatically compile it as a C++ program.

undefined reference to ‘_imp__InitBizLib’

I am getting the error above when trying to use a 3rd party SDK with a C++ program written using eclipse on XP. Based on searches I understand that this error is caused by the linker not correctly pointing to the .dll or .lib. I have included the path to these items in the Environmental Variable - PATH, and also in eclipse under Properties\C/C++ Build\Settings\MinGW C++ Linker\Libraries. The path used for both of these items is C:\Program Files\Card Scanning Solutions\SDK
Here is the console output:
**** Build of configuration Debug for project HelloWorld ****
**** Internal Builder is used for build ****
g++ -IC:\Program Files\Card Scanning Solutions\SDK -O0 -g3 -Wall -c -fmessage-length=0 -lm -v -o src\BizScan.o ..\src\BizScan.cpp
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.6.1/configure --enable-languages=c,c++,fortran,objc,obj-c++ --disable-sjlj-exceptions --with-dwarf2 --enable-shared --enable-libgomp --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --build=mingw32 --prefix=/mingw
Thread model: win32
gcc version 4.6.1 (GCC)
COLLECT_GCC_OPTIONS='-I' 'C:\Program Files\Card Scanning Solutions\SDK' '-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-o' 'src\BizScan.o' '-shared-libgcc' '-mtune=i386' '-march=i386'
c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/cc1plus.exe -quiet -v -I C:\Program Files\Card Scanning Solutions\SDK -iprefix c:\mingw\bin\../lib/gcc/mingw32/4.6.1/ -dD ..\src\BizScan.cpp -quiet -dumpbase BizScan.cpp -mtune=i386 -march=i386 -auxbase-strip src\BizScan.o -g3 -O0 -Wall -version -fmessage-length=0 -o C:\DOCUME~1\Game\LOCALS~1\Temp\ccwufpbm.s
GNU C++ (GCC) version 4.6.1 (mingw32)
compiled by GNU C version 4.6.1, GMP version 5.0.1, MPFR version 2.4.1, MPC version 0.8.1
GGC heuristics: --param ggc-min-expand=64 --param ggc-min-heapsize=65307
ignoring nonexistent directory "c:\mingw\bin\../lib/gcc/mingw32/4.6.1/../../../../mingw32/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.6.1/include/c++"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.6.1/include/c++/mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.6.1/include/c++/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.6.1/include"
ignoring duplicate directory "/mingw/lib/gcc/mingw32/4.6.1/../../../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.6.1/include-fixed"
ignoring nonexistent directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.6.1/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
C:\Program Files\Card Scanning Solutions\SDK
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include-fixed
End of search list.
GNU C++ (GCC) version 4.6.1 (mingw32)
compiled by GNU C version 4.6.1, GMP version 5.0.1, MPFR version 2.4.1, MPC version 0.8.1
GGC heuristics: --param ggc-min-expand=64 --param ggc-min-heapsize=65307
Compiler executable checksum: 8e56a7b4b7f3db333ff95dd4b2d788a4
COLLECT_GCC_OPTIONS='-I' 'C:\Program Files\Card Scanning Solutions\SDK' '-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-o' 'src\BizScan.o' '-shared-libgcc' '-mtune=i386' '-march=i386'
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/as.exe -o src\BizScan.o C:\DOCUME~1\Game\LOCALS~1\Temp\ccwufpbm.s
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/4.6.1/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/4.6.1/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../;/mingw/lib/
COLLECT_GCC_OPTIONS='-I' 'C:\Program Files\Card Scanning Solutions\SDK' '-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-o' 'src\BizScan.o' '-shared-libgcc' '-mtune=i386' '-march=i386'
g++ -LC:\Program Files\Card Scanning Solutions\SDK -lm -o HelloWorld.exe src\HelloWorld.o src\BizScan.o
src\BizScan.o: In function `ZN7BizScan4InitEv':
C:\Documents and Settings\Game\workspace\HelloWorld\Debug/../src/BizScan.cpp:20: undefined reference to `_imp__UseFixedModel'
C:\Documents and Settings\Game\workspace\HelloWorld\Debug/../src/BizScan.cpp:22: undefined reference to `_imp__SetTwainScanner'
C:\Documents and Settings\Game\workspace\HelloWorld\Debug/../src/BizScan.cpp:23: undefined reference to `_imp__InitScanLib'
C:\Documents and Settings\Game\workspace\HelloWorld\Debug/../src/BizScan.cpp:27: undefined reference to `_imp__InitImageLib'
C:\Documents and Settings\Game\workspace\HelloWorld\Debug/../src/BizScan.cpp:31: undefined reference to `_imp__InitBizLib'
src\BizScan.o: In function `~BizScan':
C:\Documents and Settings\Game\workspace\HelloWorld\Debug/../src/BizScan.cpp:39: undefined reference to `_imp__UnInitScanLib'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 5648 ms.
Here is BizScan.cpp:
/*
* BizScan.cpp
*
* Created on: Feb 13, 2012
* Author: TFB
*/
#include "BizScan.h"
#include "slibexp.h" // ScanLib SDK header file
#include "..\header_lib\BizExp.h" // ScanLib SDK header file
#include "..\header_lib\ImageExp.h" // ScanLib SDK header file
#include "CSSN.h"
#include "windef.h"
BizScan::BizScan() {
BizScan::Init();
}
const char* BizScan::Init() {
UseFixedModel(11);
const char *scannerSelected = "WIA-A6 Scanner 735";
int result = SetTwainScanner(scannerSelected);
result = InitScanLib(CSSN::GetSDKLicense().c_str());
if(result < 0 && result != -13){
return ScannerErrors(result);
} else {
result = InitImageLib(CSSN::GetSDKLicense().c_str());//initialize Image Lib SDK
if(result < 0 && result != -13)//if error
return ImageErrors(result);//call image lib sdk
result = InitBizLib(CSSN::GetSDKLicense().c_str());//initialize Biz Card Lib Scanner SDK
if(result < 0 && result != -13)//if error
return BizErrors(result);//call biz card scanner Lib SDK error handler
}
return "Successful Initialization !!";
}
BizScan::~BizScan() {
UnInitScanLib();
}
Here is BizScan.h:
/*
* BizScan.h
*
* Created on: Feb 13, 2012
* Author: TFB
*/
#ifndef BIZSCAN_H_
#define BIZSCAN_H_
#include "windef.h"
typedef HINSTANCE hDLL;
class BizScan {
private:
public:
BizScan();
virtual ~BizScan();
const char* Init() ;
};
#endif /* BIZSCAN_H_ */
And here is the relevant part of slibexp.h:
/*/////////////////////////////////////////////////////////////////////
//
// SLibExp.h
//
// Card Scanning Solutions (LLC)
// All Rights Reserved
//
/////////////////////////////////////////////////////////////////////*/
#ifndef SLIBEXP_H
#define SLIBEXP_H
#ifdef SDLL
#define SDLL_EXP __declspec( dllexport )
#else
#define SDLL_EXP __declspec( dllimport )
#endif
// Functions Prototypes
#if defined __cplusplus || defined c_plusplus
extern "C"
{
#endif
SDLL_EXP void UseFixedModel(int val);
SDLL_EXP short SetTwainScanner(const char *szStr);
SDLL_EXP short InitScanLib(const char * license);
IMAGE_EXPORT short InitImageLib(const char *licanse);
SDLL_EXP short UnInitScanLib();
#if defined __cplusplus || defined c_plusplus
};
#endif
#endif /* SLIBEXP_H */
The other two methods, referenced in the console output both are defined in other header files that are in the same folder (the project root directory). These are defined as follows:
IMAGE_EXPORT short InitImageLib(const char *licanse);
extern "C" BIZ_EXPORT short InitBizLib(const char *license);
Is there another place I need to link these .dll's. I have tried using the LoadLibrary() method, but that method gets rejected for not being part of the scope. How should I link to these dll's? Thank you for any direction you may offer.
Looking at the output you show, this is the relevant command:
g++ -LC:\Program Files\Card Scanning Solutions\SDK -lm -o HelloWorld.exe src\HelloWorld.o src\BizScan.o
It includes the path to the library, but there is no reference ro the actual library itself. I don't know what the name of the library is, but let's assume it's libBizLib.a, there should be -lBizLib at the end of that command; or if you want to link against the dll, you can use its full name (without -l).
(By the way, you mention a .lib extension of the library. That usually indicates a Visual Studio library. Are you sure the version you have, is compatible with MinGW?)
A similar issue happened to me recently. It appears to be because of the difference in compilers. Where the library was built with one version of MinGW w64, my version is different enough that I can't compile in the .a file that came with the library.
But you can use dll's at runtime using LoadLibrary, but you then have to be more explicit when calling it. Then the main ways the dll won't load is if the file isn't in the dll load path or if the dll is the wrong bit type (32 bit v 64 bit).
Hope that helps.