I am working on MacOS 13, my clang++ is Homebrew clang version 15.0.6, boost is Homebrew boost 1.81.0.
Consider the following program test.cpp:
#include <boost/program_options.hpp>
#include <iostream>
int main(int argc, char** argv){
namespace bpo = boost::program_options;
bpo::options_description options("Allowed options");
options.add_options();
bpo::variables_map options_vm;
try {
bpo::store(bpo::parse_command_line(argc, argv, options), options_vm);
bpo::notify(options_vm);
}
catch(boost::program_options::error &e){
std::cerr << e.what() << std::endl;
exit(0);
}
}
Compiling the program with
clang++ -isystem /usr/local/include -L/usr/local/lib -lboost_program_options test.cpp
works, and calling ./a.out --asdf correctly reports the mistake and exits as intended.
The libc++ the above links to (I suppose, the system version) seems not to provide support for compiling with -D_LIBCPP_ENABLE_ASSERTIONS=1. Therefore, I tried linking to the Homebrew version of libc++ and compiling with
clang++ -isystem /usr/local/include -L/usr/local/opt/llvm/lib/c++ -lc++ -Wl,-rpath,/usr/local/opt/llvm/lib/c++ -L/usr/local/lib -lboost_program_options test.cpp
When running ./a.out --asdf again, I get libc++abi: terminating with uncaught exception of type boost::wrapexcept<boost::program_options::unknown_option>.
What's going on here? Is it because the Homebrew boost is linked to the native libc++, and not the one from Homebrew LLVM? How can I resolve the issue?
Edit. One suggestion was to use
clang++ --sysroot=/usr/local/opt/llvm -I/usr/local/include -L/usr/local/lib -lboost_program_options test.cpp
This results in
In file included from test.cpp:2:
In file included from /usr/local/include/boost/program_options.hpp:15:
In file included from /usr/local/include/boost/program_options/options_description.hpp:11:
In file included from /usr/local/include/boost/program_options/config.hpp:10:
In file included from /usr/local/include/boost/config.hpp:57:
In file included from /usr/local/include/boost/config/platform/macos.hpp:28:
/usr/local/include/boost/config/detail/posix_features.hpp:18:15: fatal error: 'unistd.h' file not found
# include <unistd.h>
^~~~~~~~~~
1 error generated.
Related
I installed the MinGW version of SDL from their website.
I created a sample piece of code just to test if I could include the library without any errors.
#include <iostream>
#include <SDL.h>
using namespace std;
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL INIT FAILED" << endl;
return 1;
}
cout << "SDL INIT SUCCEEDED" << endl;
SDL_Quit();
return 0;
}
I also created a Makefile:
#OBJS specifies which files to compile as part of the project
OBJS = main.cpp
#CC specifies which compiler we're using
CC = g++
#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -Isrc\includes
#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -Lsrc\lib
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS = -w -Wl,-subsystem,windows
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = main
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
If I don't include the int argc, char* argv[] inside of int main() and try to ming32-make, it throws an error:
C:\Users\username\Documents\Projects\C++\SDL_test> mingw32-make
g++ main.cpp -Isrc\includes -Lsrc\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o main
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: src\lib/libSDL2main.a(SDL_windows_main.o): in function `main_getcmdline':
/Users/valve/release/SDL2/SDL2-2.26.2-source/foo-x64/../src/main/windows/SDL_windows_main.c:82: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [Makefile:26: all] Error 1
When I include int argc, char* argv[], it doesn't give any errors but doesn't print anything either.
C:\Users\username\Documents\Projects\C++\SDL_test> mingw32-make
g++ main.cpp -Isrc\includes -Lsrc\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o main
C:\Users\username\Documents\Projects\C++\SDL_test>
When I use make instead of mingw32-make, the output remains the same.
I am using VSCode and I have included the header files and lib files in an src folder in the same directory as my script and also moved the SDL2.dll file in the root folder:
My C++ Configuration on VSCode:
Compiler Path: C:\MinGW\bin\g++.exe
Compiler Arguments:
IntelliSense mode: gcc-x64 (legacy) // Because using anything else says the the mode is incompatible with the compiler path.
Include path:
${workspaceFolder}/**
${workspaceFolder}/src/includes
I had also recieved SDL.h: file or directory not found errors before this and I fixed them by creating the Makefile.
Is there something I'm missing? Does SDL not output to stdout, because I've seen tutorials online and they are able to get outputs from cout fine on them.
I am expecting cout to work when I run the script.
-Wl,-subsystem,windows (aka -mwindows) hides the console window, and with it all output. Remove it, and use it only in the release builds.
-w suppresses all warnings
This is extremely unwise. Prefer -Wall -Wextra -Wdeprecated to enable most common warnings, plus -std=c++20 -pedantic-errors to enforce standard compliance (replace 20 with the latest version supported by your compiler).
As suggested by #keltar, you might be able to get output even from a program built with -mwindows if you redirect it to a file, using my_program.exe >output.txt.
EDIT: The issue was that I hadn't installed SDL2 the right way. I installed MSYS2 and used it to install MinGW-w64.
I then used the Msys2 command-line interface to install SDL2 using these commands:
pacman -Syu
pacman -Su
pacman -S mingw-w64-x86_64-SDL2
This installed the header and lib files for SDL in their proper locations. I was then able to include those files in my code.
I changed the main function from int main(int argc, char* argv[]) to int WinMain(int argc, char* argv[]) because I'm on Windows and this helps get rid of the undefined reference to WinMain error.
My working code:
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int WinMain(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL INIT FAILED" << endl;
return 1;
}
cout << "SDL INIT SUCCESSFUL" << endl;
}
I am trying to use the boost library with QT on windows. I've successfully build the library and also managed to include it in my project. However, on including gmp (#include "boost/multiprecision/gmp.hpp") and creating an object (boost::multiprecision::mpz_int myint;) I get the following error:
C:\Users\Laurenz\Documents\libraries\boost_1_66_0\include\boost\multiprecision\gmp.hpp:31: error: gmp.h: No such file or directory
And indeed, I haven't been able to find any such file in the boost directory. What did I do wrong?
Install the dependency and link to it. (See What is an undefined reference/unresolved external symbol error and how do I fix it?)
Alternatively, consider not using GMP, using cpp_int.hpp instead.
Since you already installed the GMP library, here's the last step:
Live On Coliru
#include <boost/multiprecision/gmp.hpp>
#include <iostream>
int main() {
boost::multiprecision::mpz_int i("1238192389824723487823749827349879872342834792374897923479");
std::cout << pow(i, 3) << "\n";
}
Note the -lgmp flag at the end of the compile/link command:
g++ -std=c++11 -O2 -Wall -Wextra -pedantic main.cpp -o demo -lgmp
Running it:
./demo
1898298004808110659499396020993351679788129852647955073547637871096272981567489303363372689896302906549189545322451852317205769760555889831589125591739044248515246136031239
I installed VTK 7.1.1 as suggested here.
I'm working on a new project with the following code:
#include <iostream>
#include <vtk-7.1/vtkImageData.h>
#include <vtk-7.1/vtkMetaImageReader.h>
#include <vtk-7.1/vtkSmartPointer.h>
#include <vtk-7.1/vtkRenderer.h>
#include <vtk-7.1/vtkImageActor.h>
#include <vtk-7.1/vtkImageMapper3D.h>
#include <vtk-7.1/vtkRenderWindow.h>
#include <vtk-7.1/vtkRenderWindowInteractor.h>
int main()
{
vtkSmartPointer<vtkMetaImageReader> reader = vtkSmartPointer<vtkMetaImageReader>::New();
reader->SetFileName("Test.mhd");
reader->Update();
std::cout<<"Hello World";
return 0;
}
I am using the following to compile:
g++ -g -Wall -I /usr/local/include -L /usr/local/lib -o main main.cpp
However, I keep getting errors such as the following:
/usr/local/include/vtk-7.1/vtkSmartPointer.h:29: error: undefined reference to `vtkSmartPointerBase::~vtkSmartPointerBase()'
Any idea as to why? I think it might be the linking maybe?
Your assumption is correct the linker command is missing the required VTK libraries. Look in the following link for more details VTK in Makefiles.
For instance
g++ -g -Wall -I /usr/local/include -L/usr/local/lib -lvtkCommon
-lvtkFiltering -lvtkImaging -lvtkGraphics -lvtkGenericFiltering -lvtkIO
-lvtkRendering -lvtkVolumeRendering
To ease building your VTK applications you could use CMake
I am trying to get an OpenGL program working on both linux and windows.
Here's my code [file=main.cc]:
#include <iostream>
#include "GL/glew.h"
using namespace std;
int main(int argc, char *argv[]){
cout << "Hello World\n";
return 0;
}
Simple enough. I'm on Linux and using
g++ main.cc -lGL -lGLEW -lSDL2
to compile my program. It works perfectly fine and if I run ./a.out I get a Hello World on my screen.
Then I try to compile it on Linux for Windows using the command
x86_64-w64-mingw32-g++ main.cc -lGL -LGLEW -LSDL2
Then however i get the error:
main.cc:3:21: fatal error: GL/glew.h: No such file or directory
#include "GL/glew.h"
^
compilation terminated.
I've already tried adding the -I/inclulde/path option with paths like /usr/include /usr/include/GL usr/include and the like, yet nothing wants to compile.
The Libraries that I'm using (or planning to) were installed using
#apt install libgl-dev libglew-dev libsdl2-dev
Any help would be very much appreciated (although I feel this is an incredibly easy fix that I'm just too stupid to figure out on my own)
#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.