How to load a .o file at runtime in C++? - c++

I'm writing a plugin library in C++ and need to call a remote function which is not defined (only declared) at the time of compilation of the host program.
I need the host program to call a function in plugin program but I also need the flexibility of compiling host program at one time and plugin program later and get the host to run a remote function from plugin.
Here's a basic implementation of my solution:
I made these 3 files:
host.cpp:
#include "plug.h"
int main() {
cout<<"Hello World!"<<endl;
remoteFunction();
return 0;
}
plug.cpp:
#include "plug.h"
void remoteFunction() {
cout<<"in Remote"<<endl;
return;
}
plug.h:
void remoteFunction(void);
Then I compiled plug.cpp using g++ -c plug.cpp to generate plug.o.
Now I can use g++ host.cpp -o executable plug.o to get a single executable file which runs fine. But this way I need to fully define the remote function beforehand. How do I get host.cpp to compile without compiling plug.cpp first.
If there are some completely different ways to get this functionality, suggestions are most welcome.

Related

can we specify some instructions which only execute at compile time(g++ main.cpp) not at runtime ( ./a.out) ? in C/C++

#include<bits/stdc++.h>
using namespace std;
{
// only run when we press ./a.out
// instruction 1
}
{
// only run when we press g++ main.cpp or gcc main.c
// instruction 2
}
int main() {
// other part of code
}
actually I want to read/write in a .txt file. when I run g++ main.cpp new .txt file should get created and when I run ./a.out " fout<<"my data"<<endl " should get execute.
The code you write describes the behavior of the program that will be the result of the process (what the program will do when executed).
What you want is part of the build process itself (what is done in addition to compilation / linkage / etc).
If you call g++ directly from the command-line, I guess you cannot achieve your goal. You have to use CMake (or make or any other build system). The exact way to do it depends on the build system.

"undefined reference to `WinMain#16'" Error in gcc editor

I am just learning c++ and began to watch a youtube tutorial by thenewboston. Unfortunately he is using Code::Blocks while I am using gcc and I do not have the option to create new class files with a button click and so had to manually create them.
I dont understand why the same code in Code::Blocks and gcc will work in Code::Blocks but not gcc. Does gcc require different coding for the same language?
EDIT: I have downloaded and tested in Code::Blocks myself
Other questions talk of how I need to give windows an entry point, but I dont know how to do that.
Test.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
int main() {
ClassTest bo;
}
ClassTest.h Code:
#ifndef CLASSTEST_H
#define CLASSTEST_H
class ClassTest {
public:
ClassTest();
};
#endif // CLASSTEST_H
ClassTest.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
ClassTest::ClassTest() {
cout << "blah blah" << endl;
}
I'm not quite sure I understand what the question is; I'm going to take it as "how do I get these three files to build into a .exe that I can run from the Windows commmand line?"
The answer is to run something like this on the command line, in the folder with the files:
g++ -c Test.cpp -o Test.o
g++ -c ClassTest.cpp -o ClassTest.o
g++ Test.o ClassTest.o -o Test.exe
The first two commands build each CPP file into an "object file", which isn't a whole program by itself but which contains the compiled version of the code in that CPP file. The last command tells the compiler to paste together the two object files into a program, and resolve all the cross-references between them. (For example, the part where Test.cpp constructs a ClassTest object needs to end up calling the ClassTest constructor code from ClassTest.cpp.)
Code::Blocks is an IDE and works out how to build each source file in your project and link them together by itself. But if you aren't using an IDE, you need to do that in another way. You can either do it manually like this, or you can write a Makefile that will check which code files have changed and rebuild and re-link everything that depends on them when you run the make command, which is how most people do it.
As for "giving Windows an entry point", that probably refers to GUI applications that want to display windows on the screen. For console programs like the one you have written, the "entry point" is main(), and you just print stuff to the command line window. To make actual Windows-style GUI windows of your own, you need to use the Windows API, which I can't tell you much about.

JNI and shared library

Trying to create dll.
So first I created a .java file as follows:
class Main
{
static
{
System.loadLibrary("sample_dll");
}
static native void sritest();
public static void main(String [] a)
{
sritest();
}
}
Then I compile it with no error to .class.
Then using javah I created Main.h.
Then I write following code in c++.
#include <jni.h>
#include <Main.h>
JNIEXPORT void JNICALL Java_Main_sritest(JNIEnv *env,jobject obj)
{
printf("hi");
}
Then I compiled this using MinGw by the following code
g++ -c sample_dll.cpp
g++ -shared -o sample_dll.dll sample_dll.o
Then I place this in java library path.
Then I run Main.java but the following exception occurs
Unstatisfied link error; sritest ()V
I find an answer . It is i do three mistakes. When compiling with c++ we must declare my jni function in extern "C"{} block. Second when compiling in minGW compiler we should use following code and implement include folder of jdk . The code now worked and my program runs smoothly without problem in jni.
The command line code for compiling .o to .dll code is after creating sample_dll.o we must do
g++ -Wall -D_JNI_IMPLEMENTATION -Wl,--kill-at -Ic:/path_of_include_folder_in_jdk_without_drive_letter -Ic:/path_of_win32_folder_in_include_folder_in_jdk_without_drive_letter -shared -o sample_dll.dll sample_dll.o
The last mistake is i want to include Main.h compiled by javah utlity that created from my java code.
This is done by
Javac Main.java
Javah Main
Then take the header file Main.h created and place it in the c++ compiler's include folder. That in my computer c:\minGW\include .
In c++ code add additional preprocessor
#include<Main.h>
All done create dll and place it in one of default java library path . The paths can be getted by using the following function in java
System.out.println (System.getProperty ("java.library.path"));

Eclipse CDT Including Unreferenced Files in Automatically Generated Build

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

Importing a header file into a catch c++ unit testing framework

I am new to using the Catch c++ unit testing framework.
I'm trying to test a class I've built, let say for example I have two files,
a cow.cpp file and a cow.h file:
cow.h:
class Cow{
public:
Cow(std::string name);
void moo(std::string someWords);
private:
std::string name;
};
cow.cpp:
Cow::Cow(std::string cowName){
name = cowName;
}
void Cow::moo(std::string someWords){
std::cout << "my name is " << name <<" mooo " << someWords <<"\n";
}
now I want to write a test for the class, so I created a test file testCow.cpp and included the catch.h c++ single header file and the cow.h header,
but now when I try to call the method moo I get an undefined call for method, saying it doesn't exist, and the only way to solve this is to copy the entire s++ implementation into the test file, which is really not a good practice...
I am compiling my file using a standard g++ command , and am using sublime text editor to edit my file, so nothing special to include or bind the files.
Any idea why such thing might happen and how to solve it?
You need to link to your class's implementation object file.
For example, compiling your class implementation to an object file:
g++ -c -o cow.o cow.cpp
Now compiling your test program:
g++ -o testCow testCow.cpp cow.o
When building a program you need to link in all the object files that the program uses.
Alternatively you could get the compiler to do all the compiling and linking in one go by giving it all the source files:
g++ -o testCow testCow.cpp cow.cpp
But that is not how projects generally do it as it gets impractical as the project grows.