This is my main code:
#include "startup.h"
int main(int argc, char *argv[]) {
printBanner(); <-- Undefined symbol for x86_64
readParameters(argc, argv); <-- Undefined symbol for x86_64
}
Contents for startup.h:
#ifndef STARTUP_H
#define STARTUP_H
#if defined __cplusplus
extern "C" {
#endif
void printBanner(void);
void readParameters(const int argc, char *argv[]);
#if defined _cplusplus
}
#endif
#endif
The implementations of the above mentioned functions is in startup.c:
#include "startup.h"
void readParameters(const int argc, char *argv[]) {
// code
}
void printBanner() {
// code
}
All the 3 files are within the same folder. Is there anything that is missing in terms of settings? I'm using xcode for this project.
I'm calling a function that is defined in .h from my main and the header is included in the .c file where functions are implemented. Can't see anything wrong with it.
Related
For Visual Studio 2019 on Windows 10 in a new project I'm getting an unexpected LNK2019 error.
I have a test function calling a function in another project where I've included the necessary *.cpp file into the test project so I can access the internals of a DLL for testing.
test.cpp:
#include "dllsubmodule.h"
void TestCountPagesInSomething()
{
// ... setup ...
int nPages;
nPages = CountPagesInSomething(iCurrentPos, hashFoundPages, hashPageCounts, sFoundData);
}
int main(int argc, char **argv)
{
TestCountPagesInSomething();
}
dllsubmodule.h
#pragma once
int CountPagesInSomething(
int iCurrentPos,
std::map<int, ERPPageInfo>& hashFoundPages,
std::map<std::string, int>& hashPageCounts,
const wxString& sFoundData
);
dllsubmodule.cpp
#include "stdafx.h"
#include "dllsubmodule.h"
int CountPagesInSomething(
int iCurrentPos,
std::map<int, ERPPageInfo>& hashFoundPages,
std::map<std::string, int>& hashPageCounts,
const wxString& sFoundData
)
{
// implementation code here...
}
For whatever reason the linker error seems to be complaining that CountPagesInSomething() doesn't exist. I suspect I'm having trouble with name mangling variations but I've no idea how to diagnose the problem.
Note: CountPagesInSomething() is internal to the DLL and not a DLL export.
Right now this is just a console project. I'm trying to get a basic printf() test working before I figure out what kind of unit test framework to use.
I'm getting an error "Use of undeclared identifier "BUFFER_LENGTH"". I've added the header in which "BUFFER_LENGTH" has been declared. I've shown the project structure below:
Project
|_ main.c
|_ application
|_ main_functionality.c
|_ headers
|_ socket.h
main.c
#include "application/main_functionality.c"
int main(int args, char *argv[]) {
my_main(argc, argv);
return 0;
}
main_functionality.c
#include "../headers/socket.h"
unsigned char inputBuffer [BUFFER_LENGTH]; <-- ERROR!!!
int my_main(int argc, char *argv[]) {
\\ code
return 0;
}
socket.h
#if defined __cplusplus
extern "C" {
#endif
#define BUFFER_LENGTH 256
#if defined __cpluscplus
}
#endif
socket.h contains the value for BUFFER_LENGTH and I've included the filename in main_functionality.c. I still get "Use of undeclared identifier" error.
I am attempting some Linux kernel development.
I have a problem in including a header file sched.h (which exists in the path /usr/src/linux-3.12.26/kernel/sched, not in /usr/src/linux-3.12.26/include/linux ).
But when I "sudo make -C /usr/src/linux-3.12.26/ M=$(pwd) modules" I got the error "fatal error: kernel/sched/sched.h: doesn't exist"
Here is my code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/percpu.h>
#include </usr/src/linux-3.12.26/kernel/sched/sched.h>
int __init init_current(void){
int cpu_num=0;
struct task_struct *p;
for(cpu_num=0;cpu_num<8;cpu_num++)
{
p=curr_task(cpu_num);
printk(KERN_ALERT "current task on cpu %d is %d\n", cpu_num, p->pid);
}
return 0;
}
void __exit exit_current(void)
{
printk(KERN_ALERT "FINISHED!\n");
}
MODULE_LICENSE("GPL");
module_init(init_current);
module_exit(exit_current);
There may be some errors in the code. My intention is to get the current running process in different cores.
#Santosh A .Thanks for continuing concerning my puzzle.After I changed the Makefile in /usr/src/linux-3.12.26/(see my answer above,it may not be normative,but work ),the headers I include can be found.Then I get another problem. Here is my code:
#include <linux/init.h>
#include <linux/module.h>
//#include <linux/sched.h>
#include <linux/percpu.h>
#include <sched/sched.h>
static int __init init_current(void){
int cpu_num=0;
extern struct task_struct *p;
struct rq *q;
extern struct rq * cpu_rq(int);
for(cpu_num=0;cpu_num<8;cpu_num++)
{
q=cpu_rq(cpu_num);
p=q->curr;
printk(KERN_ALERT "current task on cpu %d is %d\n", cpu_num, p->pid);
}
return 0;
}
static void __exit exit_current(void)
{
printk(KERN_ALERT "FINISHED!\n");
}
MODULE_LICENSE("GPL");
module_init(init_current);
module_exit(exit_current);
Here is error info:
/home/wison/code/current/current.c: In function ‘init_current’:
kernel/sched/sched.h:539:23: error: expected identifier or ‘(’ before ‘&’ token
#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
^
/home/wison/code/current/current.c:11:28: note: in expansion of macro ‘cpu_rq’
extern struct rq * cpu_rq(int);
I have the following code:
#include <cstdlib>
#include <cstdio>
#include <atomic>
enum ATYPE { Undefined = 0, typeA, typeB, typeC };
template<ATYPE TYPE = Undefined>
struct Object
{
Object() { counter++; }
static std::atomic<int> counter;
};
//template<ATYPE TYPE>
//std::atomic<int> Object<TYPE>::counter = 0;
template<ATYPE TYPE>
void test()
{
printf("in test\n");
Object<TYPE> o;
}
int main(int argc, char **argv)
{
test<typeA>();
printf("%d\n", Object<typeA>::counter.load());
return 0;
}
and when I compile it with the following command line:
clang++ -o test -std=c++11 -stdlib=libc++ test.cpp
I got the following error:
Undefined symbols for architecture x86_64:
"Object<(ATYPE)1>::counter", referenced from:
_main in testray-D4iTOH.o
Object<(ATYPE)1>::Object() in testray-D4iTOH.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have no idea if what I am trying to do is technically possible. As the code hopefully shows, I am trying to create a static instance of the atomic class (BTW, I have no idea how to initialize this variable either. How do you intialize a static std::atomic<>?). What I am trying to do is count the number of instances of the class Object created while running the program, for each possible type (typeA, B, C, etc.).
That's the mechanism I came up with but maybe (beside the problem I have which I would like to fix if possible) someone could advice a better solution? It would be much appreciated.
Thank you so much.
As pointed by Dave in the comment, the static variable needs to be declared somewhere:
include
#include <cstdio>
#include <atomic>
enum ATYPE { Undefined = 0, typeA, typeB, typeC };
template<ATYPE TYPE = Undefined>
struct Object
{
Object() { counter++; }
static std::atomic<int> counter;
};
template<ATYPE TYPE>
std::atomic<int> Object<TYPE>::counter(0);
template<ATYPE TYPE>
void test()
{
printf("in test\n");
Object<TYPE> o;
}
int main(int argc, char **argv)
{
test<typeA>();
printf("%d\n", Object<typeA>::counter.load());
return 0;
}
It compiles fine.
I have been trying to build a device adapter for a open-source software called Micro-manager to control a microscope and there are some problems that I am facing, there are these two files (one header and the other CPP) that are already present in the open source package of Micro-Manager.
//MoudluleInterface.h
#ifndef _MODULE_INTERFACE_H_
#define _MODULE_INTERFACE_H_
#ifdef WIN32
#ifdef MODULE_EXPORTS
#define MODULE_API __declspec(dllexport)
#else
#define MODULE_API __declspec(dllimport)
#endif
#else
#define MODULE_API
#endif
#define MM_MODULE_ERR_OK 1000
#define MM_MODULE_ERR_WRONG_INDEX 1001
#define MM_MODULE_ERR_BUFFER_TOO_SMALL 1002
///////////////////////////////////////////////////////////////////////////////
// header version
// NOTE: If any of the exported module API calls changes, the interface version
// must be incremented
// new version 5 supports device discoverability
#define MODULE_INTERFACE_VERSION 7
#ifdef WIN32
const char* const LIB_NAME_PREFIX = "mmgr_dal_";
#else
const char* const LIB_NAME_PREFIX = "libmmgr_dal_";
#endif
#include "MMDevice.h"
///////////////////////////////////////////////////////////////////////////////
// Exported module interface
///////////////////////////////////////////////////////////////////////////////
extern "C" {
MODULE_API MM::Device* CreateDevice(const char* name);
MODULE_API void DeleteDevice(MM::Device* pDevice);
MODULE_API long GetModuleVersion();
MODULE_API long GetDeviceInterfaceVersion();
MODULE_API unsigned GetNumberOfDevices();
MODULE_API bool GetDeviceName(unsigned deviceIndex, char* name, unsigned bufferLength);
MODULE_API bool GetDeviceDescription(const char* deviceName, char* name, unsigned bufferLength);
And here is a part of the CPP file which defines these functions
//ModuleInterface.cpp
#define _CRT_SECURE_NO_DEPRECATE
#include "ModuleInterface.h"
#include <vector>
#include <string>
typedef std::pair<std::string, std::string> DeviceInfo;
std::vector<DeviceInfo> g_availableDevices;
int FindDeviceIndex(const char* deviceName)
{
for (unsigned i=0; i<g_availableDevices.size(); i++)
if (g_availableDevices[i].first.compare(deviceName) == 0)
return i;
return -1;
}
MODULE_API long GetModuleVersion()
{
return MODULE_INTERFACE_VERSION;
}
MODULE_API long GetDeviceInterfaceVersion()
{
return DEVICE_INTERFACE_VERSION;
}
MODULE_API unsigned GetNumberOfDevices()
{
return (unsigned) g_availableDevices.size();
}
MODULE_API bool GetDeviceName(unsigned deviceIndex, char* name, unsigned bufLen)
{
if (deviceIndex >= g_availableDevices.size())
return false;
Now the problem is it gives me an error C2491 (definition of dllimport function not allowed)
I did research about this and it usually is when a function is defined when it is supposed to be declared, I have already defined the function in ModuleInterface.h and then used it in ModuleInterface.cpp but it still shows the same error.
Can there be some other possibility for this error to occur? Or is there something wrong with the code?
You're not supposed to repeat your MODULE_API declaration in the definition, having it as part of the declaration is good enough. Remove the use of MODULE_API from the .cpp file and the code should compile.