I have something that need to link some C++ codes to main Pascal program. I followed this tutorial, now I have:
download.h
#include <iostream>
#include <curl/curl.h>
using namespace std;
// function goes here
int downloadsrc(char* pkg_name){
// do stuff
}
download.pas:
unit download;
{$link download.obj} // fpc may overwrite download.o
Interface
uses ctypes;
function downloadsrc(pkg_name:string):integer;
Implementation
// I leave this empty
end.
My main program:
program myprog;
uses
warn, download, test;
var i:integer;
begin
if ParamCount = 0 then help()
else for i:= 1 to ParamCount do
begin
if ParamStr(i) = 'download' then downloadsrc(ParamStr(i+1))
else if ParamStr(i) = 'test' then test();
end;
end.
I tried g++ to compile .h file, change the output.. but when build the program FPC says that:
download.obj: file not recognized: file format not recognized
Compiling download.pas manually still works.
Am I do something wrong here? Am I need to do some other things, like add a compile flag or modify the code?
FPC, like GCC, uses COFF objects. Make sure your .obj is COFF not OMF.
If you fix that, you probably must also link some C++ runtime library, and declare downloadsrc in a C callable way, and alter the FPC declaration to match (cdecl)
Related
I'm new to cpp in eclipse and trying to mess with simple builds. I have made a basic project which automatically generates build info (no user-defined makefile).
Simple (Working) Case
I made a "Hello World" project called Test (not an empty project). It has one file - Test.cpp with a main() in => builds and runs fine.
Test.cpp
int main() {
// output some stuff
}
Slightly More Complex (Working) Case
I make a new file called Main.cpp. Move the main() function into Main.cpp and make a decleration of a function in main too - void test();
The test() function lives in Test.cpp, which is where I provide the function definition.
Main.cpp
#include <iostream>
void test();
int main() {
// Use test()
}
Test.cpp
#include <iostream>
void test() {
// output some stuff
}
Build it, there are now two .o files in the Debug directory - Test.o and Main.o. This runs fine.
The Problem
Now I try to introduce a third file - Limits.cpp.
Limits.cpp
#include <iostream>
void printLimits() {
// Print out limits for different integer sizes
}
Again I provide a deceleration of this function in Main.cpp.
Main.cpp
#include <iostream>
void test();
void printLimits();
int main() {
// Use printLimits()
}
This time there is no object file created for Limits in the Debug folder => the build fails.
Main.cpp:*line* undefined reference to `getLimits()'
It just looks like the autogenerated build config for the project is duff. I've tried looking around the project properties but I have had no luck. I've tried including the path/file in Include Paths/Include files, other objects, and I've checked just about every other property I can see. This has been frustrating me for two days.
The strange thing about this is case 2. It looks like because Test.cpp was the first file made it always includes this in the build? (even if it is not imported). Where as because Limits.cpp is new and not imported it doesn't generate an object file so it can't link the function.
I could me making an error by needing to include the files but my understanding is if all the object files make it to the linker then all will be well (ie I just need the function declarations when making the object files which is what I have here).
With a "Blank project" it seems to compile all the cpp files even if they are never used so my case above works. (Although, it doesn't work for files in subfolders of src). Looks like its a "feature" of the "Hello world" project type but it's going to drive me crazy knowing there must be a way to get it to do this.
If anyone knows if it is possible to include this file without writing my own makefile (I'm not a makefile guru (yet!)), or let me know if this is not possible with autogen CDT that would be great.
Using MinGW GCC toolchain.
Thanks
main.cpp
#include <iostream>
#include <string>
using namespace std;
void echo(string);
int main()
{
echo("hello");
cout << "Hello world!" << endl;
return 0;
}
print.cpp
#include <iostream>
#include <string>
void echo(string code){
cout << code;
}
After compiling the code in code blocks 12.11, it gives me that error:
undefined reference to `echo(std::string)
I use windows 7 x64.
I have added the directory; Project>build options > search directories and added the current working directory.
All the files are in one console project in code blocks
I believe you should read up a bit more on namespaces usage. You are missing std in print.cpp.
Generally, while starting to learn cpp or getting a grip of the language you should always try writing full names of the classes along with the namespaces. Eventually with practice and some oversights (like now) you will learn why you really need them. In a nutshell namespaces are great:
When you are writing code over multiple files
Compartmentalize your code into separate blocks.
Also, using namespace std; should be used within cpp files mostly (otherwise headers get mangled up.
Anyways, try changing your code to this:
#include <iostream>
#include <string>
void echo(std::string code){
std::cout << code;
}
Then your results will look like this:
> g++ main.cpp print.cpp -o a.out
> ./a.out
helloHello world!
You should get more than that linker error, since you use string without any namespace in your print.cpp file. And if that source file doesn't compile it can't be linked with, and you will get the linker error you have.
Change to e.g.
void echo(std::string code) { ... }
And you do try to link with the object file created from print.cpp ?
I know this is old, but for anyone looking to solve this issue, the following may be a solution for you. If you have g++ follow c++ 11 under project->build options (check your options anyway) then you must check that box for all files you make in the project for the error to be cleared up. I had that annoying undefined reference thing too but now it is gone!
Try "Project/Properties/Build Targets tab". There you should find "Build target files" field. In that filed find "print.cpp" and click the checkbox (now the compiler will build print.cpp).
Some usefull information on Project management in CB
http://www.codeblocks.org/docs/main_codeblocks_en.html
When dealing with strings in C++ its best to sue std::string and your code seems to be wrong with a changes like using std::cout instead of plain cout another thing you need to be careful is linking your files especially files in different directories you need to tell code blocks were to find this print.cpp by going to build option and go for the search tab directory and point to where print.cpp is other wise the other approach is to just build a project which will have the main.cpp and and then add print.cpp class to current project I hope this will be of some help
For those of you that have read and done the drills from stroustrup's "programming principles and practice using c++" I am having trouble doing the first part of the chapter 8 drill. The main problem I have with this part is towards the end of the question where it states "On Windows, you need to have both use.cpp and my.cpp in a project and use { char cc; cin>>cc; } in use.cpp to be able to see your output." If we aren't allowed std_lib_facilities.h for use.cpp how do we make this happen?
Also what exactly does it mean when it says "On Windows, you need to have both use.cpp and my.cpp in a project"? Let me know if I'm looking to deeply into this.
Create three files: my.h, my.cpp, and use.cpp. The header file my.h contains
extern int foo;
void print_foo();
void print(int);
The source code file my.cpp which #include
my.h and std_lib_facilities.h, defines print_foo() to print the value of foo using cout, and print(int i) to print the value of i using cout.
The source code file use.cpp that will #include my.h, defines main() to set set the value of foo to 7 and print it using print_foo(), and to print the value 99 using print(). Note that use.cpp does not #include std_lib_facilities.h as it doesn't directly use any of those facilities.
Get these files complied and run. On Windows, you need to have both use.cpp and my.cpp in a project and use { char cc; cin>>cc; } in use.cpp to be able to see your output.
{ char cc; cin>>cc; }
Is for reading a character from standard input (waiting for input). In VS, and other IDEs, you need to do this just to see the output of the program otherwise the cmd window will close too fast to read the output. You don't need std_lib_facilities.h, just include <iostream> and write the code above at the end of the main function.
Get these files complied and run. On Windows, you need to have both
use.cpp and my.cpp in a project and use { char cc; cin>>cc; } in
use.cpp to be able to see your output.
To compile on windows, in VS or some other IDE, you need to include both source files. On linux you need both too, however, the compilation procedure (makefile or g++) explicitly requires these files so for windows these files are emphasized.
I downloaded Code::Blocks from here: http://www.codeblocks.org/downloads/26
I'm learning c programming. When I run the following program, I get error:
iostream: No such file or directory
error: syntax error before "namespace"
warning: type defaults to `int' in declaration of `std'
warning: data definition has no type or storage class
In function `main':
error: `cout' undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
error: `cin' undeclared (first use in this function)
I'm running the following program:
#include <iostream>
using namespace std;
int main()
{
int x;
x = 0;
do {
// "Hello, world!" is printed at least one time
// even though the condition is false
cout<<"Hello, world!\n";
} while ( x != 0 );
cin.get();
}
I tried Dev-C++, I get the same error.
How to fix this?
Is this in a file like "program.c" or "program.cpp"? If it's a .c file, then your compiler may be interpreting it as C, and not C++. This could easily cause such an error. It's possible to "force" the compiler to treat either such extension as the other, but by default, .c files are for C, and .cpp files are compiled as C++.
It's either this, or somehow your default "include" directories for the standard library are not set up right, but I don't know how you'd fix that, as that'd be compiler/environment dependent.
I also had that problem when trying to run my first program in Code::Blocks. My file was saved with '.c' extension as 'test.c' and when I saved it as 'test.cpp', it worked fine.
It is also worth mentioning that I had to restart Code::Blocks before new 'test.cpp' file was compiled successfully.
While saving your source code before compiling just save the name with extension ".cpp". You wont get the error..
I got the same problem.
Change #include < iostream.h >
to #incude < iostream >
Consequently, in your program, change every keyword related to iostream, such as cin cout and endl to std::cout, std::cin and std::endl
That'll do the trick
Use <iostream> instead of <iostream.h>
and add std:: before cout, cin etc
Use std::cout << "Welcome";
instead of cout << "Welcome";
Save the file with .cpp extension
you have missing iostream.h file in you mingw directory folder placed inside codeblocks/devc++. what you have to do is just download the file from link given below and replace with your previous mingw folder in codeblocks/devc++.
http://www.4shared.com/rar/owp-D0Km/mingw.html
I found the problem was cause by having a previous version of cgg and cpp in a Perl installation. The Perl structure did not have the correct library files. When I added C:\MinGW\bin and C:\MinGW\MSYS\1.0\bin to the path, I added them at the end so it picked up the Perl install first. I moved the path variable entries to the beginning and reopened my cmd window and it now works because it finds the MinGW version first.
Type path to see your path environment varialble. Mine now looks like:
C:\MinGW>path
PATH=C:\MinGW\bin;C:\MinGW\MSYS\1.0\bin;C:\Perl\site\bin;C:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\BluetoothSoftware\;
Apparently you want to create a c++ file. But you allowed your computer to auto provide the file extension C/C++. When it does that it automatically provides a file extension of ".c". Which is not corect. You want ".cpp".
Solution: Rename your file with a ".cpp" extension, or else explicitly state your extension when saving new files by putting ".cpp" (without quotes of course) after your intended file name; i.e. specify your file extension.
I tried in Dev-C++ . Instead of iostream.h use iostream also write the using namespace std;
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello World\n";
return 0;
}
you written your program in C++ code use c code then your program run correctly
in first line use it
#include <Io stream.h>
main ()
{
in ending line use it
system (pause");
You are trying to make a C game right? If you are your code is C++ not C. So if you are trying to make a C game than you should change your code. This might help.
Just put "Using namespace std;" before main() to define the scope of identifiers you are using. This will handle your problem easily.
Try including iostream.h instead of iostream.
I'm having a linking issue with a basic C++ program. No, I'm not including .cpp files!
This is what's happening.
main.cpp:
#include "header.h"
#include <iostream>
int main() {
std::cout << "Hello!";
}
header.h:
#ifndef _HEADER_H
#define _HEADER_H
class Something {
public:
printContents();
};
#endif
something.cpp:
#include "header.h"
#include <iostream>
Something::printContents() {
cout << "This class's Contents!!";
}
What's happening is that I get a compiler error going: multiple definitions of some standard C function, such as strtod:
g++ -o ... main.o
build/....main.o: In function
`strtod':
../MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdlib.h:318:
multiple definition of `strtod'
build/..something.o:...something.cpp:(.text+0x0):
first defined here collect2: ld
returned 1 exit status
If I get rid of #include <iostream> in one of the two occasions and get rid of the couts, it will compile. What's going on? I'm using g++ and NetBeans to compile.
I tried in the command line:
g++ *.h *.cpp -o program
and the same thing happened.
Please note that _HEADER_H is an illegal name in C++ user code - names beginning with the underscore and an uppercase letter are reserved for the C++ implementation. This does not normally cause noticeable problems, but when you use what may be a common name in the implementation like HEADER in this context, it well might.
Modify,
Something::printContents()
{
std::cout << "This class's Contents!!";
}
NOTE: Specify the return datatype.
One of your problems is right here:
I tried in the command line: g++ *.h
*.cpp -o program
Don't pass your header files... Try something like this:
g++ *.cpp -o program
I could not reproduce your exact problem. I get this to compile and link nicely with the following few notes:
Add a void return type to the printContents-function (So it says void printContents(); in the header and void Something::printContents() { in the implementation-file)
Use std::cout rather than just cout. cout is not defined in the scope it is used
Make sure header.h ends with a blank line
Use HEADER_H rather than _HEADER_H (like Neil Butterworth says)
I use the command line g++ main.cpp something.cpp to compile.
I see a couple of problems. You shuold define the returning value of the function
printContents()
and you must write
std::cout
if you don't write
using namespace std;
The problem was in a multi-installation of MinGW. I had one already installed, and when I got Qt on my computer, it had installed it's own MinGW. Bummer, I ported the code to my university's servers and it ran OK.
Bummer!!
Thanks everybody for the help, I will definitely follow your guidelines in the future.
Header names - no underscores
Correct return type
Real code in the forums!
Leo Bruzzaniti