compiling a Fortran program with multiple parts - fortran

I'm very very new to Fortran and the whole Intel compiler thing (I'm using the Windows-based Intel compiler with IMSL library w/o Visual Studio integration, therefore command line only).
The problem should be extremely simple: I have a Fortran program that I needed to compile that's stored as four different source code parts. The main program (and some subroutines) are stored in the code file named central.for, and there are also three files, let's call them s1.for, s2.for and s3.for, each of which contains only one subroutine (lets call them sub1-sub3) and no main program. The main program calls the subroutines stored in s1-s3 as well as in central.for.
The question is how should I compile it:
When I try to compile the central.for, it gives error LNK2019: unresolved external symbol _SUB1 referenced in function _MAIN__. Similarly it gives the same message for SUB2 and SUB3.
When I try to compile s1.for-s3.for, it gives error LNK2019: unresolved external symbol _MAIN__ referenced in function _main
It seems obvious that I need to link them somehow. However, I have no idea how

You need to compile the pieces of the program without linking first, then link them all together. So your command line will look like this:
ifort /c s1.for
ifort /c s2.for
ifort /c s3.for
ifort /c central.for
where the /c is short for /compile-only; /nolink would also work. This will generate files s1.o, s2.o, s3.o, and central.o, respectively. The above can be done in any order. Now the individual pieces get linked together, and you'll do something like
ifort /exe:central.exe central.o s1.o s2.o s3.o
to build the executable.
Note that this gets one step more complicated if you're compiling modern fortran code that uses modules, and (for instance) the main program in central.for has a line like use s2. In that case, you have to compile (even with /c) s2 before central; when you compile s2 in that case you'd get an extra output file, s2.mod. That s2.mod file would be needed to compile central.for. But given the filename extensions, this likely is an older code and modules probably aren't an issue.

This is how I have done it. Each of the supporting functions compile in a Static Library. Then in the main program project add an existing item and selected the static library .lib file (Output from compilation). The just call the functions as if their source was part of the main code (with CALL MYLIBFUN())
Good Luck.

Related

gcc compiled library: can successfully link with, how come it's "undefined symbol" when run the program?

I have a source code of a library (MP4v2 to be specific but I don't think it matters).
The original source code compiles into libmp4v2.so (with configure, make, make install), and everything works fine.
The original source code has the following line of code in "file.h",
MP4V2_EXPORT
MP4FileHandle MP4CreateEx(const char* fileName);
The implementation of this function, is in a file called "mp4.cpp".
MP4V2_EXPORT is defined to be __attribute__((visibility("default")))
The MP4CreateEx works fine, and I can use this function and link with "-lmp4v2", and run the final compiled program without any problem.
I then created another function in "file.h",
MP4V2_EXPORT
MP4FileHandle MP4CustomCreateEx(const char* fileName);
The implementation is in "mp4.cpp" too.
After configure, make, and make install, I can compile my code (which uses MP4CustomCreateEx function) and link with "-lmp4v2" without problem.
But when I run it (by "./a.out"), then it says,
./a.out: symbol lookup error: ./a.out: undefined symbol: MP4CustomCreateEx
Note that with the same lib file, I can still use MP4CreateEx function and all other exported functions without any problem, only this MP4CustomCreateEx has problem.
Any hints on what may cause the undefined symbol problem? Thanks.

IntelliSense error identifier "emlrtStack" is undefined

I am converting a MATLAB written function into C by "Matlab coder". After I get the converted files , the converted function always have first input argument as const emlrtStack *sp. Now when I am trying to test it on VC++ 2013, IntelliSense is giving mentioned above error.
I manually tried to locate this identifier in emlrt.h file but no such thing is present there. I tried to convert a simple multiply function with two input arguments[like, c=mul(a,b)] but still the converted function has this extra argument inside the function in addition to a and b.
(which means this argument is not function specific).
If someone has a solution to this or have experienced a problem like this, please share or help.
Moreover If someone know how to simply test these converted functions, it would be a much appreciated additional help .
It is likely that the code that was generated for a MEX function rather than a standalone target. MEX functions are binaries written C, C++ or Fortran that can be called like a normal MATLAB function. Generating code to produce a MEX function allows two things. First, you can test your generated code in MATLAB because you can call the MEX function from MATLAB like any other function. Look for a file named mul_mex.mex* after you do code generation and try to call it: mul_mex(1,2). The other use for generating a MEX function is that it can often be faster than the MATLAB code from which it was generated. MEX functions are only used in the context of MATLAB.
The parameter emlrtStack* that you saw appears in MEX generated code to aid in runtime error reporting. It is not present in standalone code that is designed to be run outside of MATLAB.
If you want to use the generated code in Visual Studio, or outside of MATLAB you should choose one of the standalone targets, LIB, DLL, or EXE. This page shows how to change the output type. To summarize, if using the command line you could say:
cfg = coder.config('lib'); %or 'dll' or 'exe'
codegen mul -config cfg -args {1,2}
If using the project interface, you click on the Build tab and choose static library or shared library in the "Output type" dropdown menu.
I would recommend reading this example that demonstrates how to use a generated DLL in Visual Studio.

Compile a C++ code snippet using G++ dynamically

Working on a C++ based application, it takes user input and generates a C++ function and compile it to create a .so file and links the function to the main application. Currently had to call an external command "g++" to do it. Wonder if it's possible to call some kind of function, say, "compile" which takes as input an code snippet and produces an .so. More precisely, I need a function that has the following syntax:
sizeOfObjBuf = compile(codeBuf, objBuf);
First parameter is a null terminated string containing a code snippet, the second parameter is the output buffer that hold the compiled code and it returns the size of size of compiled code.
The whole idea is to get rid of dependency on an external program (g++) so the application can run on any Linux system (even when it doesn't have g++ installed).
Thanks.
I'm afraid the answer is "no".
You could implement that function by executing G++ (or some other compiler) in a separate process and waiting for it to finish, but that still requires the user to have a compiler installed.
You can't compile C++ code without a C++ compiler.
I am not going to do the research to figure out how it is done, but I believe the LLVM C++ compiler can be used in this way. All of the parts of LLVM are designed to run as a library, in theory.
OK, a tiny bit of research and I found this: http://clang.llvm.org/docs/LibTooling.html

referenced in section `.rodata' of

Hey friends
I am stuck at a serious linking error, I am getting an error like this referenced in section .rodata' of xxx defined in discarded section .text._xxx.
This is how I am compiling my program
g++ main.o CPQENReportBB1.o CPQENReportBB2.o CPQ10minRecord.o PQ10minconfiguration.o ENReportConfig.o CPQFreeRecording.o CPQFreeHistogramReport.o
I have 8 files, it is compiling correctly but while linking I get the previously mentioned error. (i am using g++ version 3.4.3)
In two files I am getting this error message; I tried to shuffle the files in linking but then it is giving error in another two files.
Please give any suggestion to solve this. I am using c++ templates
2 possible reasons:
the file format and the machine architecture.
the use of __attribute__((section))

wxWidgets and "Implement_App" causes _main duplicate symbol error

I'm compiling a trivial wxWidgets app on MacOS X 10.6 with XCode 3.2
The linker is return an error about the symbol _main being defined twice:
once in main.mm
once in the test_app.cpp file.
After I commented out the macro:
Implement_App(TestApp)
The error went away, compiled & linked and I was able to run the application.
I haven't found this anywhere so any ideas about this?
IMPLEMENT_APP is a macro used in wxWidgets to create an entry point to the program without worrying about whether the program will be compiled on Windows, Mac, *nix, or whatever. As a result of this, IMPLEMENT_APP has to define main (or its equivalent, such as WinMain).
You might find the IMPLEMENT_APP_NO_MAIN macro to be useful. Check the other IMPLEMENT_APP_XXX functions in wx/app.h, too.
This paragraph from the wxApp overview is a little helpful too:
Note the use of IMPLEMENT_APP(appClass), which allows wxWidgets to dynamically create an instance of the application object at the appropriate point in wxWidgets initialization. Previous versions of wxWidgets used to rely on the creation of a global application object, but this is no longer recommended, because required global initialization may not have been performed at application object construction time.