UE4 Compile error with UFUNCTION(NetMulticast). error LNK2005: function already defined in *.cpp.obj - unreal-engine4

MyActor.h
UCLASS()
class FPS_API AMyActor: public AActor
{
GENERATED_BODY()
...
public:
UFUNCTION(NetMulticast, Reliable)
void MulticastRPCMyFunction();
...
}
MyActor.cpp
void AMyActor::MulticastRPCMyFunction()
{
UE_LOG(LogTemp, Log, TEXT("Message"));
}
When i compile my project, i can check the error message below.
Compile error
*.gen.cpp.obj : error LNK2005: "public: void __cdecl *::MulticastRPCMyFunction(void)" (?MulticastRPCMyFunction#*##QEAAXXZ) already defined in *.cpp.obj

With networked functions (in your case the NetMulticast metadata) you do not name the function the same thing in the Cpp file as the header file because it gets generated by UHT (hence the linker error about it already being defined).
In your case your Cpp file would need to look like this:
void AMyActor::MulticastRPCMyFunction_Implementation()
{
UE_LOG(LogTemp, Log, TEXT("Message"));
}
Notice the _Implementation addition to the function name.
If you ever add the WithValidation metadata, then you would need another function with _Validate added to the end of the function name.

Related

"DllAttribute could not be found": call a function in a .jslib file from unity

I tried to call a JavaScript Function from a .jslib file in Unity. I did it as described here
I have the .jslibe file in my assets/plugin folder. Now I tried to access it in the following code:
public class Prefab : MonoBehaviour
{
private string test = "test";
[DllImport("__Internal")]
private static extern void SendToJavscript(string test);
void Start()
{
SendToJavscript(this.test);
}
}
But it won´t compile and I get the error "The type or namespace name 'DllImportAttribute' could not be found (are you using directive or an assembly reference?)
Has anyone a idea what´s wrong here? I never got this error before..
At the top of your script add
using System.Runtime.InteropServices;

Function in drools rule file

I have a simple POJO, which has a String field
public class InputFilter {
String email;
}
And this is the DRL file
function void printEmail(String email) {
System.out.println(email);
}
rule "PrintEmail"
when
InputPayload(eval(printEmail(inputFilter.email)))
then
end
Here I get the error as
text=Rule Compilation error inputFilter.email cannot be resolved to a type
This should be simple. I am not sure what I am missing here. I have imported all the required classes. Also, it can understand the type when I do something like inputFilter.email != null.

filter("/*).through(PersistFilter.class) does not compile

In my code I have the following:
private ServletModule getModule() {
return new ServletModule() {
#Override
public void configureServlets() {
filter("/*").through(PersistFilter.class);
}
};
}
It does not compile allthough it comes straight from the guice-persist site. The error message I get from the compiler is:
Application.java:[47,28] error: cannot access Filter
Netbeans tells me this:
method FilterKeyBindingBuilder.through(Class) is not applicable
(actual argument Class cannot be converted to Class by method invocation conversion)
I checked the code of PersistFilter and it does extend Filter.
Any ideas?
This was a stupid thing. The servlet api was not being pulled in. Guice-persist needs it

Inter Type Declaration on Compiled Class File

Is it possible to do Inter Type Declarations with AspectJ on Compiled Class Files at Load Time Weaving?
As an example: I compile some Groovy code and want to add fields or methods with IDT.
Update:
Oh my goodness, you do not need reflection to access members or execute methods. Eclipse shows errors in the editor, but you may just ignore them, the code compiles and runs fine anyway. So the aspect is really much more strightforward and simple:
public aspect LTWAspect {
public static String Application.staticField = "value of static field";
public String Application.normalField = "value of normal field";
public void Application.myMethod() {
System.out.println(normalField);
}
void around() : execution(void Application.main(..)) {
System.out.println("around before");
proceed();
System.out.println("around after");
System.out.println(Application.staticField);
new Application().myMethod();
}
}
Original answer:
Yes, but you have a hen-and-egg problem there, i.e. you cannot just reference the newly introduced fields from your LTW aspect code without reflection. (The last sentence is not true, see update above.) Plus, in order to make your LTW aspect compile, you need the classes to be woven on the project's build path so as to be able to reference them. Example:
Java project
public class Application {
public static void main(String[] args) {
System.out.println("main");
}
}
AspectJ project
import org.aspectj.lang.SoftException;
public aspect LTWAspect {
public static String Application.staticField = "value of static field";
public String Application.normalField = "value of normal field";
public void Application.myMethod() {
try {
System.out.println(Application.class.getDeclaredField("normalField").get(this));
} catch (Exception e) {
throw new SoftException(e);
}
}
void around() : execution(void Application.main(..)) {
System.out.println("around before");
proceed();
System.out.println("around after");
try {
System.out.println(Application.class.getDeclaredField("staticField").get(null));
Application.class.getDeclaredMethod("myMethod", null).invoke(new Application());
} catch (Exception e) {
throw new SoftException(e);
}
}
}
So, e.g. in Eclipse you need to put the Java project on the AspectJ project's build path under "Projects" because only then it can see Java class Application on which you want to declare members. After compilation you just start the Java project and do LTW on the aspect project (don't forget an aop-ajc.xml referencing LTWAspect).
In my example above I declare a static member, a non-static ("normal") member and a non-static method. My advice prints the static member and calls the non-static method, both via reflection. The non-static method then prints the non-static member, again via reflection. This is not nice, but it works and proves the ITD in combination with LTW is possible. There might be a more elegant way, but if so I am unaware of it. (Update: There is a more elegant way: Just ignore the errors marked by Eclipse IDE, see above.)
Program output
around before
main
around after
value of static field
value of normal field

Eclipse Java JNI, LoadLibrary linking error

This my second time coding Java and never referencing any external library before. I follow the JNI examples online and I get UnsatisfiedLinkError when trying to load the dll. I thought that I have to create DLL first before trying to load, but all the examples I've looked they don't mention about creating DLL. Most of them stating that I should create Java code first then native code.
public class ClassSample1
{
public native void displayHelloWorld();
static
{
System.loadLibrary("MyLittleJNI");
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
ClassSample1 classSample1;
classSample1 = new ClassSample1();
classSample1.displayHelloWorld();
System.out.println("Hello");
}
}
How can I get ride of the error?
The sample code you provide assumes that there is a DLL in the search path called MyLittleJNI.dll, containing a method displayHelloWorld. The actual C function name in the DLL is decorated using a well defined syntax.
If you get an UnsatisfiedLinkError in loadLibrary(), it is because the JVM cannot find the DLL. You can duck the issue temporarily by specifying the full pathname to the DLL using the System.load(filename) method instead.
Once load or loadLibrary succeeds, you need to make sure that the native function is named correctly. To aid in this, you can use javah to generate a header file containing prototypes for all the native functions in a class.
More information about how to use JNI can be found in here and here.
EDIT: Also, the "Related" column to the right of this questions seems to contain several useful related question.
I try to create new project again.
So here is JNISample2.java file
public class JNISample2
{
static
{
System.loadLibrary("JNISample2Dll");
}
public native void displayHelloWorld();
public static void main(String[] args)
{
System.out.println("from java Hello");
JNISample2 JNIsample2;
JNIsample2 = new JNISample2();
JNIsample2.displayHelloWorld();
}
}
And here .h file that's generated by the javah -classpath . JNISample2
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNISample2 */
#ifndef _Included_JNISample2
#define _Included_JNISample2
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNISample2
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Here is my .h file dll that I create VS2005 with MFC app.
// JNISample2Dll.h : main header file for the JNISample2Dll DLL
//
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
#include "JNISample2.h"
// CJNISample2DllApp
// See JNISample2Dll.cpp for the implementation of this class
//
class CJNISample2DllApp : public CWinApp
{
public:
CJNISample2DllApp();
// Overrides
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld(JNIEnv *, jobject);
And here is my .cpp file
// JNISample2Dll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "JNISample2Dll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//
//TODO: If this DLL is dynamically linked against the MFC DLLs,
// any functions exported from this DLL which call into
// MFC must have the AFX_MANAGE_STATE macro added at the
// very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
// CJNISample2DllApp
BEGIN_MESSAGE_MAP(CJNISample2DllApp, CWinApp)
END_MESSAGE_MAP()
// CJNISample2DllApp construction
CJNISample2DllApp::CJNISample2DllApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only CJNISample2DllApp object
CJNISample2DllApp theApp;
// CJNISample2DllApp initialization
BOOL CJNISample2DllApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
JNIEXPORT void JNICALL Java_JNISample2_displayHelloWorld(JNIEnv *, jobject)
{
MessageBox(NULL, TEXT("In JNISample2Dll"), TEXT("DLL"), 1);
}
After I use command prompt to run: java JNISample2, it display the string "from java Hello", but how come it does not display messagebox that I put inside the .cpp DLL file?