I was trying to figure out how to link Fortran and C++ code, and one of the tutorials had written 2 programs, one in C++ in a file named testC.cpp, and the other in Fortran in a file named testF.f but I need to input the following compilation instructions:
gfortran -c testF.f
g++ -c testC.cpp
g++ -o test testF.o testC.o -lg2c
Problem is, I'm working in an IDE called Bloodshed Dev-C++ so I have no idea how to do this. I tried going in compiler options and in the general section I appended those instructions in the option "add the following commands when calling the compiler". Doesn't work.
Maybee you need use custom Makefile. Project->project options. Or include *.mak files
Related
I'm on Mac OS. I'm using Visual Studio Code.
I'm coding in C++. I recently installed the most recent version of Boost (1.76.0).
My file name: test.cpp
I've included this header in my file:
#include <boost/smart_ptr/scoped_ptr.hpp>
I'm compiling with this command:
g++ -std=c++11 test.cpp
My code won't compile. I keep getting this error:
'boost/smart_ptr/scoped_ptr.hpp' file not found
Question: What am I doing wrong?
Any help is greatly appreciated.
It's simple, whenever you use g++, you need to define your include folders with -I switch, in this case you can say:
g++ -Iboost -std=c++11 test.cpp
here boost is the name of the folder that your .h/.hpp files are inside it.
Update
Here is also a link that completely explain about how to use boost library: Link
Before you asked, yes i did look this up FOR DAYS. Im completely stuck... I'm using MINGW32 (my shortcut says MSYS) to compile my c and cpp code. For about 2 or 3 days now I have been getting strange errors. (below) It was working JUST FINE before. I even ran the same code i've compiled before and it gave the same error. I then go into DevC++ and open then compile an it works just fine.
ERRORS:
namespace: command not found
using: command not found
syntax error:
int main(){
(sometimes it gives me a big unreadable mess)
I'm really stuck... I dont want to have to switch to DevC++... I like to use my own text editor and compile in a command line.
From your errors namespace: command not found and using: command not found says to me that you aren't compiling the code with an appropriate compiler.
For reference in MinGW32 toolchain:
gcc.exe = C
g++.exe = C++
You may find it useful to take a look at what the IDE's actualy do with your compiler.
My current IDE allows me to see all the commands that it runs to build my project:
C:/mingw32/bin/g++.exe -c "C:/MyProgram/main.cpp" -g -O0 -std=c++14 -Wall -o ./Obj/main.cpp.o -I. -IDependencies/Something/include
So lets examine what this does.
My current toolchain is MinGW32 which is located in C:/mingw32/bin/g++.exe
g++ is our c++ compiler so we call g++.exe and we pass the following switch:
-c "C:/MyProgramm/main.cpp"
This tells my compiler to compile the main.cpp from my project directory. then my IDE adds a few additional command line switches. For the purpose of the answer I will only consider -o. This tells us the output file from our code main.cpp into an output file.
The reason we produce such a file is to save us time compiling so that we do not have to compile the same file twice without making changes to it. We perform this step on each of our files creating a collection of .o files.
The new file is then saved in "C:/MyProgramm/Obj/main.cpp.o"
Which means that your command line function will look something like this:
C:/mingw32/bin/g++.exe -c "<my project directory>/<file>.cpp" -o ./Obj/<file>.cpp.o
I would recommend that you read up on documentation for the g++ function and learn from different IDE's as you will soon find that you need to do more advanced things with your compiler.
For example to enable features from c++14 I add -std=c++14
*Edited to reflect feedback.
I am learning from The C++ Primer. One of the exercises is to compile a program with arguments in main(). For this I am trying to use mac terminal.
I need to compile a C++11 Unix executable file named main which takes f as an argument. I am using Xcode 4.6.3 on OS X Lion.
I compiled the program with clang++ -std=c++11 -stdlib=libc++ main.cpp -o main.
But don’t know what to do next.
I found -frecord-gcc-switches while searching compiler options on google. It does what I need to do. Is there a clang version of this?
Please use simple language. I have never used command line before. I tried going through the clang manual but a lot of it is out of my depth.
If the compiler didn't complain about anything, you should have a fresh new file named main in the same directory as the source file, and you can run it from the command line using ./main -f or the like.
I installed the following:
MINGW32_NT-6.1 i686 Msys
I am working with the command line.
Wrote the "typical" HelloWorld.cpp program.
IF I compile with: cpp HelloWorld.cpp -o HelloWorld.exe COMPILE is good. (18k)
BUT execution fails: 16 bit MS-DOS Subsystem. NTVDM CPU error
IF I compile with: g++ HelloWorld.cpp -o HelloWorld.exe COMPILE is good. (48k)
Execution is good.
I cannot determine the BEST way to execute the compile and what the difference is between the methods. Any suggestions? or good references?
THANKS.
"cpp" is the "C PreProcessor", not the compiler. So you're just getting something strange in HelloWorld.exe
Execute the "type HelloWorld.exe" and see what it gives. It shouldn't even be a binary file - just a long text file with all the "#includes" and "#defines" replaced.
To your question - the second way is "right", because you actually invoke the compiler/linker and produce a valid executable. The first "way" is a valid command, but it has almost nothing to do with compilation and linking.
I am trying to integrate CUDA and openCV in a project. Problem is openCV won't compile when NVCC is used, while a normal c++ project compiles just fine. This seems odd to me, as I thought NVCC passed all host code to the c/c++ compiler, in this case the visual studio compiler.
The errors I get are?
c:\opencv2.0\include\opencv\cxoperations.hpp(1137): error: no operator "=" matches these operands
operand types are: const cv::Range = cv::Range
c:\opencv2.0\include\opencv\cxoperations.hpp(2469): error: more than one instance of overloaded function "std::abs" matches the argument list:
function "abs(long double)"
function "abs(float)"
function "abs(double)"
function "abs(long)"
function "abs(int)"
argument types are: (ptrdiff_t)
So my question is why the difference considering the same compiler (should be) is being used and secondly how I could remedy this.
In general I would recommend keeping separation between host code and CUDA code, only using nvcc for the kernels and host "wrappers". This is particularly easy with Visual Studio, create your project as normal (e.g. a Console application) and then implement your application in .cpp files. When you want to run a CUDA function, create the kernel and a wrapper in one or more .cu files. The Cuda.rules file provided with the SDK will automatically enable VS to compile the .cu files and link the result with the rest of the .cpp files.
NVCC passes C++ code through to the host compiler, but it has to first parse and understand the code. Unfortunately, NVCC has troubles with STL. If at all possible, separate code that makes use of STL into .cpp files and have those compiled with Visual Studio (without passing them first through NVCC).
Compile the .cu code as a library and then link it to the main program. I suggest to use cmake as it makes the process a breeze
There's a project hosted at cuda-grayscale that shows how to integrate OpenCV + CUDA together. If you download the sources check the Makefile:
g++ $(CFLAGS) -c main.cpp -o Debug/main.o
nvcc $(CUDAFLAGS) -c kernel_gpu.cu -o Debug/kernel_gpu.o
g++ $(LDFLAGS) Debug/main.o Debug/kernel_gpu.o -o Debug/grayscale
It's a very simple project that demonstrates how to separate regular C++ code (OpenCV and stuff) from the CUDA code and compile them.
So there's no easy way to use nvcc to compile your current C++ code, you have to write wrappers and compile them using nvcc, while you compile the rest of your code using g++ or what-have-you?